Skip to content
Open
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: 4 additions & 0 deletions resend/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def __init__(
self.error_type = error_type
self.headers = headers or {}

def __reduce__(self) -> Any:
# Exception.args does not contain all of our constructor arguments.
return (Exception.__new__, (type(self), *self.args), self.__dict__)


class MissingApiKeyError(ResendError):
"""see https://resend.com/docs/api-reference/errors"""
Expand Down
29 changes: 29 additions & 0 deletions tests/exceptions_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pickle
import unittest

import pytest
Expand Down Expand Up @@ -130,3 +131,31 @@ def test_headers_on_application_error(self) -> None:
headers=headers,
)
assert e.value.headers == headers


@pytest.mark.parametrize( # type: ignore[untyped-decorator]
"protocol", range(pickle.HIGHEST_PROTOCOL + 1)
)
@pytest.mark.parametrize( # type: ignore[untyped-decorator]
("code", "error_type"),
[
(999, "unknown"),
(400, "validation_error"),
(401, "missing_api_key"),
(403, "invalid_api_key"),
(422, "missing_required_field"),
(429, "rate_limit_exceeded"),
(500, "application_error"),
],
)
def test_error_pickle_round_trip(code: int, error_type: str, protocol: int) -> None:
with pytest.raises(ResendError) as caught:
raise_for_code_and_type(code, error_type, "msg", headers={"retry-after": "5"})

error = caught.value
restored = pickle.loads(pickle.dumps(error, protocol=protocol))

assert type(restored) is type(error)
assert restored.args == error.args
assert str(restored) == str(error)
assert restored.__dict__ == error.__dict__