Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions src/ApiErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,31 @@
namespace OpenPix\PhpSdk;

use Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ApiErrorException extends Exception
{
private RequestInterface $request;
private ResponseInterface $response;

public static function from(string $errorMessage, RequestInterface $request, ResponseInterface $response)

Check failure on line 14 in src/ApiErrorException.php

View workflow job for this annotation

GitHub Actions / Lint code and fix code style

Method OpenPix\PhpSdk\ApiErrorException::from() has no return type specified.
{
$exception = new self($errorMessage);

$exception->request = $request;
$exception->response = $response;

return $exception;
}

public function getHttpRequest(): RequestInterface
{
return $this->request;
}

public function getHttpResponse(): ResponseInterface
{
return $this->response;
}
}
6 changes: 3 additions & 3 deletions src/RequestTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function transport($request): array

$response = $this->httpClient->sendRequest($request);

return $this->hydrateResponse($response);
return $this->hydrateResponse($request, $response);
}

/**
Expand All @@ -127,7 +127,7 @@ private function withRequestDefaultParameters(RequestInterface $request): Reques
*
* @return array<string, mixed>
*/
private function hydrateResponse(ResponseInterface $response): array
private function hydrateResponse(RequestInterface $request, ResponseInterface $response): array
{
$contents = json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR);

Expand All @@ -142,7 +142,7 @@ private function hydrateResponse(ResponseInterface $response): array
$error = $error["message"];
}

throw new ApiErrorException($error);
throw ApiErrorException::from($error, $request, $response);
}

return $contents;
Expand Down
16 changes: 14 additions & 2 deletions tests/RequestTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,15 @@ private function testApiErrorHandlingFor($error): void
$requestMock
->method("withAddedHeader")
->willReturn($requestMock);
$requestMock
->method("getRequestTarget")
->willReturn("target");

$encodedResponse = json_encode(["error" => $error]);

$responseMock = $this->createConfiguredMock(ResponseInterface::class, [
"getBody" => $this->createConfiguredMock(StreamInterface::class, [
"getContents" => json_encode(["error" => $error]),
"getContents" => $encodedResponse,
]),
"getStatusCode" => 400,
"getReasonPhrase" => "Bad request",
Expand All @@ -112,6 +117,13 @@ private function testApiErrorHandlingFor($error): void
$this->createMock(StreamFactoryInterface::class),
);

$requestTransport->transport($requestMock);
try {
$requestTransport->transport($requestMock);
} catch (ApiErrorException $e) {
$this->assertSame($e->getHttpRequest()->getRequestTarget(), "target");
$this->assertSame($e->getHttpResponse()->getBody()->getContents(), $encodedResponse);

throw $e;
}
}
}
Loading