feature #30567 [HttpClient] exceptions carry response (antonch1989)

This PR was squashed before being merged into the 4.3-dev branch (closes #30567).

Discussion
----------

[HttpClient] exceptions carry response

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #30502
| License       | MIT
| Doc PR        |

Commits
-------

103448cc67 [HttpClient] exceptions carry response
This commit is contained in:
Fabien Potencier 2019-03-19 08:49:53 +01:00
commit bff9e68bb4
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
{
}