PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / libraries / http / client-common / Plugin / ErrorPlugin.php

ErrorPlugin.php in DecaLog 4.4.0, at includes/libraries/http/client-common/Plugin/ErrorPlugin.php

93 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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