[HttpClient] exceptions carry response

This commit is contained in:
Anton Chernikov 2019-03-14 13:49:27 +03:00 committed by Fabien Potencier
parent 8af6395fa8
commit 103448cc67
5 changed files with 37 additions and 3 deletions

View File

@ -20,8 +20,11 @@ use Symfony\Contracts\HttpClient\ResponseInterface;
*/
trait HttpExceptionTrait
{
private $response;
public function __construct(ResponseInterface $response)
{
$this->response = $response;
$code = $response->getInfo('http_code');
$url = $response->getInfo('url');
$message = sprintf('HTTP %d returned for URL "%s".', $code, $url);
@ -35,4 +38,9 @@ trait HttpExceptionTrait
parent::__construct($message, $code);
}
public function getResponse(): ResponseInterface
{
return $this->response;
}
}

View File

@ -18,6 +18,6 @@ namespace Symfony\Contracts\HttpClient\Exception;
*
* @experimental in 1.1
*/
interface ClientExceptionInterface extends ExceptionInterface
interface ClientExceptionInterface extends HttpExceptionInterface
{
}

View File

@ -0,0 +1,26 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Contracts\HttpClient\Exception;
use Symfony\Contracts\HttpClient\ResponseInterface;
/**
* Base interface for HTTP-related exceptions.
*
* @author Anton Chernikov <anton_ch1989@mail.ru>
*
* @experimental in 1.1
*/
interface HttpExceptionInterface extends ExceptionInterface
{
public function getResponse(): ResponseInterface;
}

View File

@ -18,6 +18,6 @@ namespace Symfony\Contracts\HttpClient\Exception;
*
* @experimental in 1.1
*/
interface RedirectionExceptionInterface extends ExceptionInterface
interface RedirectionExceptionInterface extends HttpExceptionInterface
{
}

View File

@ -18,6 +18,6 @@ namespace Symfony\Contracts\HttpClient\Exception;
*
* @experimental in 1.1
*/
interface ServerExceptionInterface extends ExceptionInterface
interface ServerExceptionInterface extends HttpExceptionInterface
{
}