|
3 | 3 | namespace Spawnia\Sailor\Error;
|
4 | 4 |
|
5 | 5 | use Psr\Http\Message\ResponseInterface;
|
| 6 | +use const JSON_PRETTY_PRINT; |
6 | 7 |
|
7 | 8 | class UnexpectedResponse extends \Exception
|
8 | 9 | {
|
9 | 10 | public int $statusCode = 0;
|
10 | 11 |
|
11 | 12 | public string $responseBody = '';
|
12 | 13 |
|
13 |
| - /** @var array<string, list<string>> */ |
| 14 | + /** @var array<string, array<string>> */ |
14 | 15 | public array $responseHeaders;
|
15 | 16 |
|
16 |
| - public static function statusCode(ResponseInterface $response): self |
| 17 | + public function __construct(ResponseInterface $response) |
17 | 18 | {
|
18 | 19 | $statusCode = $response->getStatusCode();
|
19 | 20 | $responseBody = $response->getBody()->__toString();
|
| 21 | + $responseHeaders = $response->getHeaders(); |
20 | 22 |
|
21 |
| - $jsonEncodedHeaders = \Safe\json_encode($response->getHeaders(), JSON_PRETTY_PRINT); |
| 23 | + $this->statusCode = $statusCode; |
| 24 | + $this->responseBody = $responseBody; |
| 25 | + $this->responseHeaders = $responseHeaders; |
22 | 26 |
|
23 |
| - $self = new self( |
24 |
| - "Unexpected HTTP status code received: {$statusCode}. Reason: \n{$responseBody}\nHeaders:\n{$jsonEncodedHeaders}", |
| 27 | + parent::__construct( |
| 28 | + "Unexpected response received: {$statusCode}. Reason: \n{$responseBody}\nHeaders:\n" . \Safe\json_encode($responseHeaders, JSON_PRETTY_PRINT), |
25 | 29 | );
|
26 |
| - $self->statusCode = $statusCode; |
27 |
| - $self->responseHeaders = $response->getHeaders(); // @phpstan-ignore assign.propertyType |
28 |
| - $self->responseBody = $responseBody; |
29 |
| - |
30 |
| - return $self; |
31 | 30 | }
|
| 31 | + |
| 32 | + |
32 | 33 | }
|
0 commit comments