Skip to content

fix: map 401 and 403 validation_error to ValidationError - #305

Open
m4xkushnir wants to merge 1 commit into
resend:mainfrom
m4xkushnir:fix/map-validation-error-401-403
Open

m4xkushnir wants to merge 1 commit into
resend:mainfrom
m4xkushnir:fix/map-validation-error-401-403

Conversation

@m4xkushnir

@m4xkushnir m4xkushnir commented Oct 22, 2026 •

Copy link
Copy Markdown

The error docs return validation_error with 400, 401 and 403.
401 is "API key is invalid" on sending endpoints, and 403 covers "The domain.com domain is not verified" and
the testing-mode recipient restriction. ERRORS only has validation_error under 400 and 422, so the 401 and 403
responses come back as a plain ResendError with an empty suggested_action, and except resend.ValidationError
doesn't catch them. An unverified sending domain is probably the most common setup mistake, so it's a shame
it's the one that slips through.

This adds validation_error to the 401 and 403 entries, plus two tests next to the existing 400 and 422 ones.
It's backward compatible because ValidationError subclasses ResendError, and code, message, error_type
and headers stay the same.

I noticed this while writing the error handling for moving Ventute (which sends all its email through Resend)
from raw httpx calls to the SDK.

One thing I left alone on purpose. The docs no longer list invalid_api_key, which is the only name
InvalidApiKeyError is registered under, so I don't think that class gets raised anymore. Whether an invalid key
should raise InvalidApiKeyError instead of ValidationError felt like your call, so this PR just follows the
same name, same class rule. Happy to change it if you'd prefer the other way.

Testing

Both new tests fail on main and pass here. The full suite passes on Python 3.14 and 3.8 (853 tests), and flake8
and mypy show nothing new.


Summary by cubic

Maps validation_error responses from 401 (invalid API key on sending endpoints) and 403 (unverified domain, testing-mode recipient restriction) to ValidationError so they carry a suggested_action and can be caught with except resend.ValidationError instead of falling back to a plain ResendError.

  • Backward compatible: ValidationError subclasses ResendError, and code, message, error_type, and headers stay the same.
  • invalid_api_key is left untouched; the Resend error docs only list validation_error for 401/403, so InvalidApiKeyError may no longer be raised.

Written for commit 7646b70. Summary will update on new commits.

View guided diff

@m4xkushnir

Copy link
Copy Markdown
Author

Hi! I use Resend for all of Ventute's email and ran into this while going through the SDK's error handling. It's a small one, so happy to adjust anything, including the InvalidApiKeyError question if you'd rather go that way. Thanks for the SDK!

@github-actions github-actions Bot added the linear-synced PR has been synced to Linear label Oct 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linear-synced PR has been synced to Linear

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant