Add response body to InvalidStatusException

This commit is contained in:
Frédéric Guillot 2018-03-02 13:41:37 -08:00
parent ebe04e672c
commit f92eb448cb
2 changed files with 12 additions and 6 deletions

View File

@ -164,7 +164,6 @@ class Client extends Base
$startTime = microtime(true);
$stream = @fopen(trim($url), 'r', false, stream_context_create($this->getContext($method, $content, $headers, $raiseForErrors)));
$response = '';
if (! is_resource($stream)) {
$this->logger->error('HttpClient: request failed ('.$url.')');
@ -176,14 +175,14 @@ class Client extends Base
return '';
}
$response = stream_get_contents($stream);
$body = stream_get_contents($stream);
$metadata = stream_get_meta_data($stream);
if ($raiseForErrors && array_key_exists('wrapper_data', $metadata)) {
$statusCode = $this->getStatusCode($metadata['wrapper_data']);
if ($statusCode >= 400) {
throw new InvalidStatusException('Request failed with status code '.$statusCode, $statusCode);
throw new InvalidStatusException('Request failed with status code '.$statusCode, $statusCode, $body);
}
}
@ -192,11 +191,11 @@ class Client extends Base
$this->logger->debug('HttpClient: headers='.var_export($headers, true));
$this->logger->debug('HttpClient: payload='.$content);
$this->logger->debug('HttpClient: metadata='.var_export($metadata, true));
$this->logger->debug('HttpClient: response='.$response);
$this->logger->debug('HttpClient: body='.$body);
$this->logger->debug('HttpClient: executionTime='.(microtime(true) - $startTime));
}
return $response;
return $body;
}
/**

View File

@ -5,15 +5,22 @@ namespace Kanboard\Core\Http;
class InvalidStatusException extends ClientException
{
protected $statusCode = 0;
protected $body = '';
public function __construct($message, $statusCode)
public function __construct($message, $statusCode, $body)
{
parent::__construct($message);
$this->statusCode = $statusCode;
$this->body = $body;
}
public function getStatusCode()
{
return $this->statusCode;
}
public function getBody()
{
return $this->body;
}
}