Skip to content

Commit

Permalink
fix: exception tweaks and example
Browse files Browse the repository at this point in the history
  • Loading branch information
drish committed Apr 22, 2024
1 parent 42db6eb commit af76d52
Show file tree
Hide file tree
Showing 3 changed files with 30 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}")
7 changes: 5 additions & 2 deletions resend/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def __init__(
"400": {"validation_error": ValidationError},
"422": {
"missing_required_fields": MissingRequiredFieldsError,
"validation_error": ValidationError
"validation_error": ValidationError,
},
"401": {"missing_api_key": MissingApiKeyError},
"403": {"invalid_api_key": InvalidApiKeyError},
Expand Down Expand Up @@ -213,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=""
)
9 changes: 7 additions & 2 deletions tests/exceptions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pytest

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


Expand All @@ -27,3 +27,8 @@ 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 af76d52

Please sign in to comment.