Skip to content

Commit

Permalink
fix: exceptions changes
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Apr 22, 2024
1 parent 821f055 commit 42db6eb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions resend/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ def __init__(
# Dict with error code -> error type mapping
ERRORS: Dict = {
"400": {"validation_error": ValidationError},
"422": {"missing_required_fields": MissingRequiredFieldsError},
"422": {
"missing_required_fields": MissingRequiredFieldsError,
"validation_error": ValidationError
},
"401": {"missing_api_key": MissingApiKeyError},
"403": {"invalid_api_key": InvalidApiKeyError},
"500": {"application_error": ApplicationError},
Expand Down Expand Up @@ -196,7 +199,7 @@ def raise_for_code_and_type(code: str, error_type: str, message: str) -> None:
error = ERRORS.get(str(code))

# Handle the case where the error might be unknown
if error is None or error.get(error_type) is None:
if error is None:
raise ResendError(
code=code, message=message, error_type=error_type, suggested_action=""
)
Expand Down
11 changes: 11 additions & 0 deletions tests/exceptions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from resend.exceptions import (MissingApiKeyError, ResendError,
ValidationError,
raise_for_code_and_type)


Expand All @@ -16,3 +17,13 @@ def test_raise_known_error(self):
with pytest.raises(MissingApiKeyError) as e:
raise_for_code_and_type(401, "missing_api_key", "err")
assert e.type is MissingApiKeyError

def test_validation_error_from_422(self):
with pytest.raises(ValidationError) as e:
raise_for_code_and_type(422, "validation_error", "err")
assert e.type is ValidationError

def test_validation_error_from_400(self):
with pytest.raises(ValidationError) as e:
raise_for_code_and_type(400, "validation_error", "err")
assert e.type is ValidationError

0 comments on commit 42db6eb

Please sign in to comment.