diff --git a/composer.json b/composer.json index 975a2f4..9bccf37 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,9 @@ ], "require": { "php": "^8.1.0", - "guzzlehttp/guzzle": "^7.5" + "guzzlehttp/guzzle": "^7.8.2 || ^8.0", + "guzzlehttp/psr7": "^2.6.3 || ^3.0", + "psr/http-client": "^1.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.13", diff --git a/src/Transporters/HttpTransporter.php b/src/Transporters/HttpTransporter.php index 0957f95..8f13f39 100644 --- a/src/Transporters/HttpTransporter.php +++ b/src/Transporters/HttpTransporter.php @@ -81,7 +81,9 @@ private function sendRequest(Closure $callable): ResponseInterface return $callable(); } catch (ClientExceptionInterface $clientException) { if ($clientException instanceof ClientException) { - $this->throwIfJsonError($clientException->getResponse(), $clientException->getResponse()->getBody()->getContents()); + $response = $clientException->getResponse(); + + $this->throwIfJsonError($response, (string) $response->getBody()); } throw new TransporterException($clientException); diff --git a/tests/Transporters/HttpTransporter.php b/tests/Transporters/HttpTransporter.php index 0344ab4..4c69875 100644 --- a/tests/Transporters/HttpTransporter.php +++ b/tests/Transporters/HttpTransporter.php @@ -1,8 +1,10 @@ 'test@resend.com']); + $request = new Request('POST', 'https://api.resend.com/email'); + $body = Utils::streamFor(json_encode(['error' => 'Unauthorized'])); + + $body->getContents(); + + $response = new Response(401, ['Content-Type' => 'application/json'], $body); + + $this->client + ->shouldReceive('sendRequest') + ->once() + ->andThrow(new ClientException('Unauthorized', $request, $response)); + + $this->http->request($payload); +})->throws(ErrorException::class, 'Unauthorized'); + test('request can handle serialization errors', function () { $payload = Payload::create('email', ['to' => 'test@resend.com']); $response = new Response(200, ['content-type' => 'text/plain'], 'err');