Make error responses accessable (#312)

This commit is contained in:
Tobias Nyholm 2017-03-26 17:26:57 +02:00 committed by GitHub
parent 511ad186ce
commit 70d467955a

View File

@ -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);
$this->response = $response;
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;
}
}