mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-26 06:46:08 +03:00
Make error responses accessable (#312)
This commit is contained in:
parent
511ad186ce
commit
70d467955a
@ -22,6 +22,11 @@ final class HttpClientException extends \RuntimeException implements Exception
|
||||
*/
|
||||
private $response;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $responseBody;
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
@ -30,7 +35,16 @@ final class HttpClientException extends \RuntimeException implements Exception
|
||||
public function __construct($message, $code, ResponseInterface $response = null)
|
||||
{
|
||||
parent::__construct($message, $code);
|
||||
|
||||
if ($response) {
|
||||
$this->response = $response;
|
||||
$body = $response->getBody()->__toString();
|
||||
if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
|
||||
$this->responseBody['message'] = $body;
|
||||
} else {
|
||||
$this->responseBody = json_decode($body, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function badRequest(ResponseInterface $response = null)
|
||||
@ -60,4 +74,12 @@ final class HttpClientException extends \RuntimeException implements Exception
|
||||
{
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getResponseBody()
|
||||
{
|
||||
return $this->responseBody;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user