Skip to content

Commit

Permalink
feat: introduce UnexpectedResponse exception
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored Nov 8, 2024
1 parent e5d12bb commit 4e63b68
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Introduce UnexpectedResponse exception

## v0.34.0

### Changed
Expand Down
44 changes: 44 additions & 0 deletions src/Error/UnexpectedResponse.php
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,
);
}
}
4 changes: 3 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psr\Http\Message\ResponseInterface;
use Safe\Exceptions\JsonException;
use Spawnia\Sailor\Error\InvalidDataException;
use Spawnia\Sailor\Error\UnexpectedResponse;

/**
* Represents a response sent by a GraphQL server.
Expand All @@ -29,10 +30,11 @@ class Response
/** This entry, if set, must have a map as its value. */
public ?\stdClass $extensions;

/** @throws UnexpectedResponse */
public static function fromResponseInterface(ResponseInterface $response): self
{
if ($response->getStatusCode() !== 200) {
throw new InvalidDataException("Response must have status code 200, got: {$response->getStatusCode()}");
throw UnexpectedResponse::statusCode($response);
}

return self::fromJson(
Expand Down
7 changes: 6 additions & 1 deletion tests/Unit/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Spawnia\Sailor\Error\InvalidDataException;
use Spawnia\Sailor\Error\UnexpectedResponse;
use Spawnia\Sailor\Response;
use Spawnia\Sailor\Tests\TestCase;

Expand Down Expand Up @@ -33,10 +34,14 @@ public function testFromResponseInterfaceNon200(): void
{
/** @var MockObject&ResponseInterface $httpResponse */
$httpResponse = self::createMock(ResponseInterface::class);
$httpResponse->method('getHeaders')
->willReturn([]);
$httpResponse->method('getBody')
->willReturn(self::createMock(StreamInterface::class));
$httpResponse->method('getStatusCode')
->willReturn(500);

self::expectException(InvalidDataException::class);
self::expectException(UnexpectedResponse::class);
Response::fromResponseInterface($httpResponse);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/generate-and-approve-examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
shell_exec("rm -rf {$expectedPath}");

$generatedPath = Examples::generatedPath($example);
shell_exec("cp --recursive {$generatedPath} {$expectedPath}");
// Using -R over --recursive for macOS compatibility
shell_exec("cp -R {$generatedPath} {$expectedPath}");
}

0 comments on commit 4e63b68

Please sign in to comment.