Update forbidden response, set message from response body content.

This commit is contained in:
Littlesqx 2019-08-28 16:28:36 +08:00 committed by David Garcia
parent 300dcc18bb
commit e0cb8023a7

View File

@ -85,7 +85,17 @@ final class HttpClientException extends \RuntimeException implements Exception
public static function forbidden(ResponseInterface $response)
{
return new self('Forbidden', 403, $response);
$body = $response->getBody()->__toString();
if (0 !== strpos($response->getHeaderLine('Content-Type'), 'application/json')) {
$validationMessage = $body;
} else {
$jsonDecoded = json_decode($body, true);
$validationMessage = isset($jsonDecoded['Error']) ? $jsonDecoded['Error'] : $body;
}
$message = sprintf("Forbidden!\n\n%s", $validationMessage);
return new self($message, 403, $response);
}
public function getResponse(): ?ResponseInterface