diff --git a/aws_lambda_powertools/utilities/parser/parser.py b/aws_lambda_powertools/utilities/parser/parser.py index 3d2f236c75e..fd0b298bd7f 100644 --- a/aws_lambda_powertools/utilities/parser/parser.py +++ b/aws_lambda_powertools/utilities/parser/parser.py @@ -4,7 +4,7 @@ import typing from typing import TYPE_CHECKING, Any, Callable, overload -from pydantic import PydanticSchemaGenerationError, ValidationError +from pydantic import PydanticSchemaGenerationError from aws_lambda_powertools.middleware_factory import lambda_handler_decorator from aws_lambda_powertools.utilities.parser.exceptions import InvalidEnvelopeError, InvalidModelTypeError @@ -108,7 +108,7 @@ def handler(event: Order, context: LambdaContext): logger.debug(f"Calling handler {handler.__name__}") return handler(parsed_event, context, **kwargs) - except (ValidationError, AttributeError) as exc: + except AttributeError as exc: raise InvalidModelTypeError(f"Error: {str(exc)}. Please ensure the type you're trying to parse into is correct") @@ -199,7 +199,7 @@ def handler(event: Order, context: LambdaContext): # when we pass a data structure that does not match the model (trying to parse a true/false/etc into a model) except PydanticSchemaGenerationError as exc: raise InvalidModelTypeError(f"The event supplied is unable to be validated into {type(model)}") from exc - except ValidationError as exc: + except AttributeError as exc: raise InvalidModelTypeError( f"Error: {str(exc)}. Please ensure the Input model inherits from BaseModel,\n" "and your payload adheres to the specified Input model structure.\n" diff --git a/tests/functional/parser/test_parser.py b/tests/functional/parser/test_parser.py index 15ac834256b..d4208c203a2 100644 --- a/tests/functional/parser/test_parser.py +++ b/tests/functional/parser/test_parser.py @@ -3,6 +3,7 @@ import pydantic import pytest +from pydantic import ValidationError from typing_extensions import Annotated from aws_lambda_powertools.utilities.parser import ( @@ -18,7 +19,7 @@ def test_parser_unsupported_event(dummy_schema, invalid_value): def handle_no_envelope(event: Dict, _: LambdaContext): return event - with pytest.raises(exceptions.InvalidModelTypeError): + with pytest.raises(ValidationError): handle_no_envelope(event=invalid_value, context=LambdaContext()) @@ -75,7 +76,7 @@ def validate_field(cls, value): assert event_parsed.version == int(event_raw["version"]) -@pytest.mark.parametrize("invalid_schema", [str, False, [], ()]) +@pytest.mark.parametrize("invalid_schema", [False, [], ()]) def test_parser_with_invalid_schema_type(dummy_event, invalid_schema): @event_parser(model=invalid_schema) def handle_no_envelope(event: Dict, _: LambdaContext): @@ -120,6 +121,15 @@ def handler(evt: dummy_schema, _: LambdaContext): handler(dummy_event["payload"], LambdaContext()) +def test_parser_event_with_payload_not_match_schema(dummy_event, dummy_schema): + @event_parser(model=dummy_schema) + def handler(event, _): + assert event.message == "hello world" + + with pytest.raises(ValidationError): + handler({"project": "powertools"}, LambdaContext()) + + @pytest.mark.parametrize( "test_input,expected", [