From e3a1e13ead6b2fc425deee053eb1dc082d8f2352 Mon Sep 17 00:00:00 2001 From: mayankbohradev Date: Sat, 11 Jul 2026 11:06:48 +0530 Subject: [PATCH] fix: map missing_required_field to MissingRequiredFieldsError The Resend API and docs emit singular missing_required_field on 422 responses. The SDK only registered the plural key, so callers catching MissingRequiredFieldsError never matched real API errors. --- resend/exceptions.py | 5 ++++- tests/exceptions_test.py | 20 +++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/resend/exceptions.py b/resend/exceptions.py index 41af790..4ceba35 100644 --- a/resend/exceptions.py +++ b/resend/exceptions.py @@ -199,6 +199,8 @@ def __init__( ERRORS: Dict[str, Dict[str, Any]] = { "400": {"validation_error": ValidationError}, "422": { + # API docs use singular `missing_required_field`; keep plural as alias. + "missing_required_field": MissingRequiredFieldsError, "missing_required_fields": MissingRequiredFieldsError, "validation_error": ValidationError, }, @@ -232,7 +234,8 @@ def raise_for_code_and_type( or ValidationError: If the error type is validation_error or - MissingRequiredFieldsError: If the error type is missing_required_fields + MissingRequiredFieldsError: If the error type is missing_required_field + (or legacy missing_required_fields) or MissingApiKeyError: If the error type is missing_api_key or diff --git a/tests/exceptions_test.py b/tests/exceptions_test.py index 39256ee..91de8bf 100644 --- a/tests/exceptions_test.py +++ b/tests/exceptions_test.py @@ -3,7 +3,8 @@ import pytest from resend.exceptions import (ApplicationError, MissingApiKeyError, - RateLimitError, ResendError, ValidationError, + MissingRequiredFieldsError, RateLimitError, + ResendError, ValidationError, raise_for_code_and_type) @@ -23,6 +24,23 @@ def test_validation_error_from_422(self) -> None: raise_for_code_and_type(422, "validation_error", "err") assert e.type is ValidationError + def test_missing_required_field_singular(self) -> None: + # Resend API docs use singular `missing_required_field`. + with pytest.raises(MissingRequiredFieldsError) as e: + raise_for_code_and_type(422, "missing_required_field", "Missing `to` field") + assert e.type is MissingRequiredFieldsError + assert e.value.error_type == "missing_required_field" + assert e.value.code == 422 + + def test_missing_required_fields_plural_alias(self) -> None: + # Keep plural as a backward-compatible alias. + with pytest.raises(MissingRequiredFieldsError) as e: + raise_for_code_and_type( + 422, "missing_required_fields", "Missing `to` field" + ) + assert e.type is MissingRequiredFieldsError + assert e.value.error_type == "missing_required_fields" + def test_validation_error_from_400(self) -> None: with pytest.raises(ValidationError) as e: raise_for_code_and_type(400, "validation_error", "err")