Skip to content
Draft
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
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions tests/Transporters/HttpTransporter.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Client\ClientInterface;
use Resend\Enums\Transporter\ContentType;
use Resend\Exceptions\ErrorException;
Expand Down Expand Up @@ -106,6 +108,23 @@
});
});

test('request can handle client exception json errors from the full response body', function () {
$payload = Payload::create('email', ['to' => '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');
Expand Down