generated from spawnia/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce UnexpectedResponse exception
- Loading branch information
Showing
5 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Spawnia\Sailor\Error; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class UnexpectedResponse extends \Exception | ||
{ | ||
public int $statusCode; | ||
|
||
public string $responseBody; | ||
|
||
/** @var array<string, array<string>> */ | ||
public array $responseHeaders; | ||
|
||
/** @param array<string, array<string>> $responseHeaders */ | ||
public function __construct( | ||
string $message, | ||
int $statusCode, | ||
string $responseBody, | ||
array $responseHeaders | ||
) { | ||
parent::__construct($message); | ||
$this->statusCode = $statusCode; | ||
$this->responseBody = $responseBody; | ||
$this->responseHeaders = $responseHeaders; | ||
} | ||
|
||
public static function statusCode(ResponseInterface $response): self | ||
{ | ||
$statusCode = $response->getStatusCode(); | ||
$responseBody = $response->getBody()->__toString(); | ||
$responseHeaders = $response->getHeaders(); | ||
|
||
$jsonEncodedHeaders = \Safe\json_encode($responseHeaders, JSON_PRETTY_PRINT); | ||
|
||
return new self( | ||
"Unexpected HTTP status code received: {$statusCode}.\nReason:\n{$responseBody}\nHeaders:\n{$jsonEncodedHeaders}", | ||
$statusCode, | ||
$responseBody, | ||
$responseHeaders, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters