mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-22 12:36:02 +03:00
Change exception message of HttpClientException when badRequest to us… (#466)
* 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
This commit is contained in:
parent
07da83776a
commit
02e3191bc9
@ -55,7 +55,21 @@ final class HttpClientException extends \RuntimeException implements Exception
|
|||||||
|
|
||||||
public static function badRequest(ResponseInterface $response = null)
|
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)
|
public static function unauthorized(ResponseInterface $response = null)
|
||||||
|
35
tests/Exception/HttpClientExceptionTest.php
Normal file
35
tests/Exception/HttpClientExceptionTest.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Mailgun
|
||||||
|
*
|
||||||
|
* This software may be modified and distributed under the terms
|
||||||
|
* of the MIT license. See the LICENSE file for details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Mailgun\Tests\Exception;
|
||||||
|
|
||||||
|
use GuzzleHttp\Psr7\Response;
|
||||||
|
use Mailgun\Exception\HttpClientException;
|
||||||
|
use Mailgun\Tests\MailgunTestCase;
|
||||||
|
|
||||||
|
class HttpClientExceptionTest extends MailgunTestCase
|
||||||
|
{
|
||||||
|
public function testBadRequestGetMessageJson()
|
||||||
|
{
|
||||||
|
$response = new Response(400, ['Content-Type' => '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'], '<html><body>Server HTML</body></html>');
|
||||||
|
$exception = HttpClientException::badRequest($response);
|
||||||
|
$this->assertStringEndsWith('<html><body>Server HTML</body></html>', $exception->getMessage());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user