Skip to content

Commit b4dde1f

Browse files
committed
fixes exception handling in parse
1 parent d16f089 commit b4dde1f

File tree

1 file changed

+5
-2
lines changed
  • aws_lambda_powertools/utilities/parser

1 file changed

+5
-2
lines changed

aws_lambda_powertools/utilities/parser/parser.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import typing
33
from typing import Any, Callable, Dict, Optional, Type, overload
44

5-
from pydantic import TypeAdapter, ValidationError
5+
from pydantic import PydanticSchemaGenerationError, TypeAdapter, ValidationError
66

77
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
88
from aws_lambda_powertools.utilities.parser.envelopes.base import Envelope
@@ -189,7 +189,10 @@ def handler(event: Order, context: LambdaContext):
189189

190190
return adapter.validate_python(event)
191191

192-
except Exception as exc:
192+
# Pydantic raises PydanticSchemaGenerationError when the model is not a Pydantic model
193+
# This is seen in the tests where we pass a non-Pydantic model type to the parser or
194+
# when we pass a data structure that does not match the model (trying to parse a true/false/etc into a model)
195+
except (ValidationError, PydanticSchemaGenerationError) as exc:
193196
raise InvalidModelTypeError(
194197
f"Error: {str(exc)}. Please ensure the Input model inherits from BaseModel,\n"
195198
"and your payload adheres to the specified Input model structure.\n"

0 commit comments

Comments
 (0)