Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.add error handling in http adapter #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Exception/AdapterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace SmsSender\Exception;

class AdapterException extends RuntimeException
{
/**
* @var array
*/
protected $data = [];

/**
* @return array
*/
public function getData()
{
return $this->data;
}

/**
* @param array $data
*/
public function setData(array $data)
{
$this->data = $data;
}
}
24 changes: 20 additions & 4 deletions src/HttpAdapter/BuzzHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace SmsSender\HttpAdapter;

use Buzz\Browser;
use SmsSender\Exception\AdapterException;

/**
* @author Kévin Gomez <[email protected]>
Expand Down Expand Up @@ -44,12 +45,15 @@ public function getContent($url, $method = 'GET', array $headers = array(), $dat
}

try {
$response = $this->browser->call($url, $method, $headers, $data);
if($response = $this->browser->call($url, $method, $headers, $data)){
return $response->getContent();
}
} catch (\Exception $e) {
return null;
if(!empty($e->getMessage())){
throw new AdapterException($e->getMessage(), $e->getCode(), $e);
}
}

return $response ? $response->getContent() : null;
throw new AdapterException((string)$this->browser->getLastResponse(), ($this->browser->getLastResponse()) ? $this->browser->getLastResponse()->getStatusCode() : 0);
}

/**
Expand All @@ -59,6 +63,18 @@ public function getName()
{
return 'buzz';
}

/**
* Return last request as string or object
*
* @param bool|false $string
*
* @return mixed|string
*/
public function getLastRequest($string = false)
{
return ($string) ? (string)$this->browser->getLastRequest() : $string;
}
}

// vim: set softtabstop=4 tabstop=4 shiftwidth=4 autoindent:
40 changes: 40 additions & 0 deletions src/HttpAdapter/CurlHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@

namespace SmsSender\HttpAdapter;

use SmsSender\Exception\AdapterException;

/**
* @author Kévin Gomez <[email protected]>
*/
class CurlHttpAdapter extends AbstractHttpAdapter implements HttpAdapterInterface
{
/**
* @var array
*/
protected $lastRequest;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -48,6 +55,19 @@ public function getContent($url, $method = 'GET', array $headers = array(), $dat
// execute the request
$content = curl_exec($c);

$this->setLastRequest([
'url' => $url,
'method' => $method,
'headers' => $headers,
'data' => $data,
]);

if(curl_errno($c)){
$adapterException = new AdapterException(curl_error($c), curl_errno($c));
$adapterException->setData(curl_getinfo($c));
curl_close($c);
throw $adapterException;
}
curl_close($c);

if (false === $content) {
Expand All @@ -64,6 +84,26 @@ public function getName()
{
return 'curl';
}

/**
* @param bool|false $string
*
* @return mixed|string
*/
public function getLastRequest($string = false)
{
return ($string) ? json_encode($this->lastRequest) : $this->lastRequest;
}

/**
* Return last request as string or object
*
* @param array $lastRequest
*/
protected function setLastRequest($lastRequest)
{
$this->lastRequest = $lastRequest;
}
}

// vim: set softtabstop=4 tabstop=4 shiftwidth=4 autoindent:
9 changes: 9 additions & 0 deletions src/HttpAdapter/HttpAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public function getContent($url, $method = 'GET', array $headers = array(), $dat
* @return string
*/
public function getName();

/**
* Return last request as string or object
*
* @param bool|false $string
*
* @return mixed|string
*/
public function getLastRequest($string = false);
}

// vim: set softtabstop=4 tabstop=4 shiftwidth=4 autoindent: