Skip to content

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

Merged
dielduarte merged 4 commits into
resend:mainfrom
m4xkushnir:fix/map-validation-error-401-403
Nov 9, 2026
Merged

dielduarte merged 4 commits into
resend:mainfrom
m4xkushnir:fix/map-validation-error-401-403

Conversation

@m4xkushnir

@m4xkushnir m4xkushnir commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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) and 403 (unverified domain, testing-mode restriction) to ValidationError so they raise a catchable error with a suggested_action instead of falling back to a plain ResendError. Backward compatible: ValidationError subclasses ResendError, and code, message, error_type, and headers stay the same. Also updates ValidationError's suggested_action to a generic message, since validation_error now covers auth and domain problems, not just missing fields. invalid_api_key is left untouched; docs only list validation_error for 401/403, so InvalidApiKeyError may no longer be raised. Bumps the package version to 2.53.0.

Written for commit 1a8be33. Summary will update on new commits.

View guided diff

@m4xkushnir

Copy link
Copy Markdown
Contributor 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
@m4xkushnir

Copy link
Copy Markdown
Contributor Author

@felipefreitag @dielduarte when you have a moment, could one of you take a look? It's a small change: 401 and 403 validation_error responses now raise ValidationError (an unverified sending domain is the common case), with tests for both. I left the open InvalidApiKeyError question in the description for you to decide. Thanks!

@m4xkushnir
m4xkushnir force-pushed the fix/map-validation-error-401-403 branch from 7646b70 to bdc3f96 Compare October 28, 2026 15:33
@codecov-commenter

codecov-commenter commented Nov 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.62%. Comparing base (7f1acd0) to head (1a8be33).
⚠️ Report is 251 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main     #305       +/-   ##
===========================================
+ Coverage   82.66%   97.62%   +14.96%     
===========================================
  Files           4       89       +85     
  Lines          75     5521     +5446     
===========================================
+ Hits           62     5390     +5328     
- Misses         13      131      +118     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…ases

validation_error now also covers 401 (invalid API key) and 403 (unverified
domain, testing-mode recipient, inbox limit), so pointing at missing
fields is misleading.

@dielduarte dielduarte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the contribution!!

@dielduarte
dielduarte merged commit df63cf5 into resend:main Nov 9, 2026
10 checks passed
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.

3 participants