Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce UnexpectedResponse exception #114

Merged
merged 10 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
39 changes: 39 additions & 0 deletions src/Error/UnexpectedResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types=1);

namespace Spawnia\Sailor\Error;

use Psr\Http\Message\ResponseInterface;

class UnexpectedResponse extends \Exception
{
public int $statusCode = 0;

public string $responseBody = '';

/** @var array<string, list<string>> */
public array $responseHeaders;
simPod marked this conversation as resolved.
Show resolved Hide resolved

public static function statusCode(
spawnia marked this conversation as resolved.
Show resolved Hide resolved
ResponseInterface $response
): self {
simPod marked this conversation as resolved.
Show resolved Hide resolved
$statusCode = $response->getStatusCode();
$responseBody = $response->getBody()->__toString();

$jsonEncodedHeaders = \Safe\json_encode($response->getHeaders(), JSON_PRETTY_PRINT);

$self = new self(
\Safe\sprintf(
"Unexpected HTTP status code received: %d. Reason: \n%s\nHeaders:\n"
. $jsonEncodedHeaders,
$statusCode,
$responseBody,
),
simPod marked this conversation as resolved.
Show resolved Hide resolved
);
$self->statusCode = $statusCode;
/** @phpstan-ignore assign.propertyType */
$self->responseHeaders = $response->getHeaders();
simPod marked this conversation as resolved.
Show resolved Hide resolved
$self->responseBody = $responseBody;

return $self;
}
}
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
2 changes: 1 addition & 1 deletion tests/generate-and-approve-examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
shell_exec("rm -rf {$expectedPath}");

$generatedPath = Examples::generatedPath($example);
shell_exec("cp --recursive {$generatedPath} {$expectedPath}");
shell_exec("cp -R {$generatedPath} {$expectedPath}");
spawnia marked this conversation as resolved.
Show resolved Hide resolved
}
Loading