Skip to content

Commit

Permalink
feat: Exceptions examples + some small tweaks (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
drish authored Apr 22, 2024
1 parent 821f055 commit 0b072b4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
18 changes: 18 additions & 0 deletions examples/with_exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import os

import resend

if not os.environ["RESEND_API_KEY"]:
raise EnvironmentError("RESEND_API_KEY is missing")

params: resend.Emails.SendParams = {
"sender": "[email protected]",
"to": ["invalid"],
"subject": "hi",
"html": "<strong>hello, world!</strong>",
}

try:
email = resend.Emails.send(params)
except resend.exceptions.ResendError as e:
print(f"Error sending email: {e}")
12 changes: 9 additions & 3 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 All @@ -210,4 +213,7 @@ def raise_for_code_and_type(code: str, error_type: str, message: str) -> None:
message=message,
error_type=error_type,
)
raise TypeError("Error type not found")
# defaults to ResendError if finally can't find error type
raise ResendError(
code=code, message=message, error_type=error_type, suggested_action=""
)
18 changes: 17 additions & 1 deletion tests/exceptions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import pytest

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


Expand All @@ -16,3 +17,18 @@ 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

def test_error_500(self):
with pytest.raises(ApplicationError) as e:
raise_for_code_and_type(500, "application_error", "err")
assert e.type is ApplicationError

0 comments on commit 0b072b4

Please sign in to comment.