Skip to content

Commit c27377f

Browse files
committed
rework constructor
1 parent d37835a commit c27377f

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/Error/UnexpectedResponse.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,31 @@
33
namespace Spawnia\Sailor\Error;
44

55
use Psr\Http\Message\ResponseInterface;
6+
use const JSON_PRETTY_PRINT;
67

78
class UnexpectedResponse extends \Exception
89
{
910
public int $statusCode = 0;
1011

1112
public string $responseBody = '';
1213

13-
/** @var array<string, list<string>> */
14+
/** @var array<string, array<string>> */
1415
public array $responseHeaders;
1516

16-
public static function statusCode(ResponseInterface $response): self
17+
public function __construct(ResponseInterface $response)
1718
{
1819
$statusCode = $response->getStatusCode();
1920
$responseBody = $response->getBody()->__toString();
21+
$responseHeaders = $response->getHeaders();
2022

21-
$jsonEncodedHeaders = \Safe\json_encode($response->getHeaders(), JSON_PRETTY_PRINT);
23+
$this->statusCode = $statusCode;
24+
$this->responseBody = $responseBody;
25+
$this->responseHeaders = $responseHeaders;
2226

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),
2529
);
26-
$self->statusCode = $statusCode;
27-
$self->responseHeaders = $response->getHeaders(); // @phpstan-ignore assign.propertyType
28-
$self->responseBody = $responseBody;
29-
30-
return $self;
3130
}
31+
32+
3233
}

src/Response.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Response
3434
public static function fromResponseInterface(ResponseInterface $response): self
3535
{
3636
if ($response->getStatusCode() !== 200) {
37-
throw UnexpectedResponse::statusCode($response);
37+
throw new UnexpectedResponse($response);
3838
}
3939

4040
return self::fromJson(

0 commit comments

Comments
 (0)