From 02e3191bc923e4f9dba4fac272e5de96b7a0fa78 Mon Sep 17 00:00:00 2001 From: Paul R Rogers Date: Sat, 5 Jan 2019 06:52:40 -0500 Subject: [PATCH] =?UTF-8?q?Change=20exception=20message=20of=20HttpClientE?= =?UTF-8?q?xception=20when=20badRequest=20to=20us=E2=80=A6=20(#466)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change exception message of HttpClientException when badRequest to use server content's message to aid resolving problems with user input. * style(HttpClientException): Removed space to comply with styleCI. * Fix missing assoc flag. * Refactor to reuse constructor parsing when passing along server error. Add tests covering passing along of server message. * Updated tests * cs * bugfix --- src/Mailgun/Exception/HttpClientException.php | 16 ++++++++- tests/Exception/HttpClientExceptionTest.php | 35 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/Exception/HttpClientExceptionTest.php diff --git a/src/Mailgun/Exception/HttpClientException.php b/src/Mailgun/Exception/HttpClientException.php index 5c0eb1d..da50873 100644 --- a/src/Mailgun/Exception/HttpClientException.php +++ b/src/Mailgun/Exception/HttpClientException.php @@ -55,7 +55,21 @@ final class HttpClientException extends \RuntimeException implements Exception public static function badRequest(ResponseInterface $response = null) { - return new self('The parameters passed to the API were invalid. Check your inputs!', 400, $response); + $message = 'The parameters passed to the API were invalid. Check your inputs!'; + + if (null !== $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['message']) ? $jsonDecoded['message'] : $body; + } + + $message = sprintf("%s\n\n%s", $message, $validationMessage); + } + + return new self($message, 400, $response); } public static function unauthorized(ResponseInterface $response = null) diff --git a/tests/Exception/HttpClientExceptionTest.php b/tests/Exception/HttpClientExceptionTest.php new file mode 100644 index 0000000..33e53d8 --- /dev/null +++ b/tests/Exception/HttpClientExceptionTest.php @@ -0,0 +1,35 @@ + 'application/json'], '{"message":"Server Message"}'); + $exception = HttpClientException::badRequest($response); + $this->assertStringEndsWith('Server Message', $exception->getMessage()); + + $response = new Response(400, ['Content-Type' => 'application/json'], '{"Foo":"Server Message"}'); + $exception = HttpClientException::badRequest($response); + $this->assertStringEndsWith('{"Foo":"Server Message"}', $exception->getMessage()); + } + + public function testBadRequestGetMessage() + { + $response = new Response(400, ['Content-Type' => 'text/html'], 'Server HTML'); + $exception = HttpClientException::badRequest($response); + $this->assertStringEndsWith('Server HTML', $exception->getMessage()); + } +}