From e0cb8023a77c69259a13b2533b955d4ca05c52b5 Mon Sep 17 00:00:00 2001 From: Littlesqx Date: Wed, 28 Aug 2019 16:28:36 +0800 Subject: [PATCH] Update forbidden response, set message from response body content. --- src/Exception/HttpClientException.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Exception/HttpClientException.php b/src/Exception/HttpClientException.php index 621077c..261d451 100644 --- a/src/Exception/HttpClientException.php +++ b/src/Exception/HttpClientException.php @@ -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