Skip to content

Commit b21dce6

Browse files
authored
Merge pull request #25 from resendlabs/add-rate-limit-error
add support for rate limit error response
2 parents be19af3 + aa36360 commit b21dce6

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/Transporters/HttpTransporter.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,37 @@ protected function throwIfJsonError(ResponseInterface $response, string $content
6969
try {
7070
$response = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
7171

72-
$errors = ['missing_required_fields', 'missing_required_field', 'missing_api_key', 'invalid_api_key', 'invalid_from_address', 'validation_error', 'not_found', 'method_not_allowed', 'invalid_scope', 'restricted_api_key', 'internal_server_error'];
7372
if (
7473
isset($response['error']) ||
75-
in_array($response['name'], $errors)
74+
$this->isResendError($response['name'])
7675
) {
7776
throw new ErrorException($response['error'] ?? $response);
7877
}
7978
} catch (JsonException $jsonException) {
8079
throw new UnserializableResponse($jsonException);
8180
}
8281
}
82+
83+
/**
84+
* Determine if the given error name is a Resend error.
85+
*/
86+
protected function isResendError(string $errorName): bool
87+
{
88+
$errors = [
89+
'missing_required_fields',
90+
'missing_required_field',
91+
'invalid_from_address',
92+
'validation_error',
93+
'missing_api_key',
94+
'invalid_api_key',
95+
'restricted_api_key',
96+
'invalid_scope',
97+
'rate_limit_exceeded',
98+
'not_found',
99+
'method_not_allowed',
100+
'internal_server_error',
101+
];
102+
103+
return in_array($errorName, $errors);
104+
}
83105
}

tests/Transporters/HttpTransporter.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,23 @@
9595
$this->http->request($payload);
9696
})->throws(UnserializableResponse::class, 'Syntax error');
9797

98-
test('throw json errors', function () {
98+
test('request can throw resend errors', function () {
99+
$payload = Payload::create('email', ['to' => '[email protected]']);
100+
$response = new Response(422, [], json_encode([
101+
'statusCode' => 422,
102+
'name' => 'missing_required_field',
103+
'message' => 'Missing `to` field',
104+
]));
105+
106+
$this->client
107+
->shouldReceive('sendRequest')
108+
->once()
109+
->andReturn($response);
110+
111+
$this->http->request($payload);
112+
})->throws(ErrorException::class);
113+
114+
test('request can throw json error', function () {
99115
$payload = Payload::create('email', ['to' => '[email protected]']);
100116
$response = new Response(422, [], 'err');
101117

0 commit comments

Comments
 (0)