Skip to content

Commit 5e02582

Browse files
author
Ana Falcao
committed
remove error handling from decorator
1 parent 4955d88 commit 5e02582

File tree

1 file changed

+16
-29
lines changed
  • aws_lambda_powertools/utilities/parser

1 file changed

+16
-29
lines changed

aws_lambda_powertools/utilities/parser/parser.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import typing
55
from typing import TYPE_CHECKING, Any, Callable, overload
66

7-
from pydantic import PydanticSchemaGenerationError, ValidationError
7+
from pydantic import PydanticSchemaGenerationError
88

99
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
1010
from aws_lambda_powertools.utilities.parser.exceptions import InvalidEnvelopeError, InvalidModelTypeError
@@ -87,8 +87,8 @@ def handler(event: Order, context: LambdaContext):
8787
When input event does not conform with the provided model
8888
InvalidModelTypeError
8989
When the model given does not implement BaseModel, is not provided
90-
TypeError, ValueError
91-
When there's an error during model instantiation, re-raised as InvalidModelTypeError
90+
InvalidEnvelopeError
91+
When envelope given does not implement BaseEnvelope
9292
"""
9393

9494
if model is None:
@@ -103,23 +103,14 @@ def handler(event: Order, context: LambdaContext):
103103
"or as the type hint of `event` in the handler that it wraps",
104104
)
105105

106-
try:
107-
if envelope:
108-
parsed_event = parse(event=event, model=model, envelope=envelope)
109-
else:
110-
parsed_event = parse(event=event, model=model)
111-
112-
logger.debug(f"Calling handler {handler.__name__}")
113-
return handler(parsed_event, context, **kwargs)
114-
except ValidationError:
115-
# Raise Pydantic validation errors as is
116-
raise
117-
except InvalidModelTypeError:
118-
# Raise invalid model type errors as is
119-
raise
120-
except (TypeError, ValueError) as exc:
121-
# Catch type and value errors that might occur during model instantiation
122-
raise InvalidModelTypeError(f"Error: {str(exc)}. Please ensure the type you're trying to parse into is correct")
106+
if envelope:
107+
parsed_event = parse(event=event, model=model, envelope=envelope)
108+
else:
109+
parsed_event = parse(event=event, model=model)
110+
111+
logger.debug(f"Calling handler {handler.__name__}")
112+
return handler(parsed_event, context, **kwargs)
113+
123114

124115
@overload
125116
def parse(event: dict[str, Any], model: type[T]) -> T: ... # pragma: no cover
@@ -180,8 +171,8 @@ def handler(event: Order, context: LambdaContext):
180171
When input event does not conform with model provided
181172
InvalidModelTypeError
182173
When model given does not implement BaseModel
183-
TypeError, ValueError
184-
When there's an error during model instantiation, re-raised as InvalidModelTypeError
174+
InvalidEnvelopeError
175+
When envelope given does not implement BaseEnvelope
185176
"""
186177
if envelope and callable(envelope):
187178
try:
@@ -200,19 +191,15 @@ def handler(event: Order, context: LambdaContext):
200191
logger.debug("Parsing and validating event model; no envelope used")
201192

202193
return _parse_and_validate_event(data=event, adapter=adapter)
203-
204-
except ValidationError:
205-
# Raise validation errors as is
206-
raise
207-
194+
208195
# Pydantic raises PydanticSchemaGenerationError when the model is not a Pydantic model
209196
# This is seen in the tests where we pass a non-Pydantic model type to the parser or
210197
# when we pass a data structure that does not match the model (trying to parse a true/false/etc into a model)
211198
except PydanticSchemaGenerationError as exc:
212199
raise InvalidModelTypeError(f"The event supplied is unable to be validated into {type(model)}") from exc
213-
except (TypeError, ValueError) as exc:
200+
except AttributeError as exc:
214201
raise InvalidModelTypeError(
215202
f"Error: {str(exc)}. Please ensure the Input model inherits from BaseModel,\n"
216203
"and your payload adheres to the specified Input model structure.\n"
217204
f"Model={model}",
218-
) from exc
205+
)

0 commit comments

Comments
 (0)