| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Http\Client\Common\Plugin; |
| 6 |
|
| 7 |
use Http\Client\Common\Exception\ClientErrorException; |
| 8 |
use Http\Client\Common\Exception\ServerErrorException; |
| 9 |
use Http\Client\Common\Plugin; |
| 10 |
use Http\Promise\Promise; |
| 11 |
use Psr\Http\Message\RequestInterface; |
| 12 |
use Psr\Http\Message\ResponseInterface; |
| 13 |
use Symfony\Component\OptionsResolver\OptionsResolver; |
| 14 |
|
| 15 |
/** |
| 16 |
* Throw exception when the response of a request is not acceptable. |
| 17 |
* |
| 18 |
* Status codes 400-499 lead to a ClientErrorException, status 500-599 to a ServerErrorException. |
| 19 |
* |
| 20 |
* Warning |
| 21 |
* ======= |
| 22 |
* |
| 23 |
* Throwing an exception on a valid response violates the PSR-18 specification. |
| 24 |
* This plugin is provided as a convenience when writing a small application. |
| 25 |
* When providing a client to a third party library, this plugin must not be |
| 26 |
* included, or the third party library will have problems with error handling. |
| 27 |
* |
| 28 |
* @author Joel Wurtz <joel.wurtz@gmail.com> |
| 29 |
*/ |
| 30 |
final class ErrorPlugin implements Plugin |
| 31 |
{ |
| 32 |
/** |
| 33 |
* @var bool Whether this plugin should only throw 5XX Exceptions (default to false). |
| 34 |
* |
| 35 |
* If set to true 4XX Responses code will never throw an exception |
| 36 |
*/ |
| 37 |
private $onlyServerException; |
| 38 |
|
| 39 |
/** |
| 40 |
* @param array{'only_server_exception'?: bool} $config |
| 41 |
* |
| 42 |
* Configuration options: |
| 43 |
* - only_server_exception: Whether this plugin should only throw 5XX Exceptions (default to false) |
| 44 |
*/ |
| 45 |
public function __construct(array $config = []) |
| 46 |
{ |
| 47 |
$resolver = new OptionsResolver(); |
| 48 |
$resolver->setDefaults([ |
| 49 |
'only_server_exception' => false, |
| 50 |
]); |
| 51 |
$resolver->setAllowedTypes('only_server_exception', 'bool'); |
| 52 |
$options = $resolver->resolve($config); |
| 53 |
|
| 54 |
$this->onlyServerException = $options['only_server_exception']; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* {@inheritdoc} |
| 59 |
*/ |
| 60 |
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise |
| 61 |
{ |
| 62 |
$promise = $next($request); |
| 63 |
|
| 64 |
return $promise->then(function (ResponseInterface $response) use ($request) { |
| 65 |
return $this->transformResponseToException($request, $response); |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Transform response to an error if possible. |
| 71 |
* |
| 72 |
* @param RequestInterface $request Request of the call |
| 73 |
* @param ResponseInterface $response Response of the call |
| 74 |
* |
| 75 |
* @return ResponseInterface If status code is not in 4xx or 5xx return response |
| 76 |
* |
| 77 |
* @throws ClientErrorException If response status code is a 4xx |
| 78 |
* @throws ServerErrorException If response status code is a 5xx |
| 79 |
*/ |
| 80 |
private function transformResponseToException(RequestInterface $request, ResponseInterface $response): ResponseInterface |
| 81 |
{ |
| 82 |
if (!$this->onlyServerException && $response->getStatusCode() >= 400 && $response->getStatusCode() < 500) { |
| 83 |
throw new ClientErrorException($response->getReasonPhrase(), $request, $response); |
| 84 |
} |
| 85 |
|
| 86 |
if ($response->getStatusCode() >= 500 && $response->getStatusCode() < 600) { |
| 87 |
throw new ServerErrorException($response->getReasonPhrase(), $request, $response); |
| 88 |
} |
| 89 |
|
| 90 |
return $response; |
| 91 |
} |
| 92 |
} |
| 93 |
|