Skip to content

Commit

Permalink
refactor(parser): Improve error message when parsing models and envel…
Browse files Browse the repository at this point in the history
…opes (#3587)

* Adding more verbose to Parser error message

* Addressing Ruben's feedback
  • Loading branch information
leandrodamascena authored Jan 5, 2024
1 parent 67d3537 commit aacb37d
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions aws_lambda_powertools/utilities/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,12 @@ def handler(event: Order, context: LambdaContext):
try:
logger.debug(f"Parsing and validating event model with envelope={envelope}")
return envelope().parse(data=event, model=model)
except AttributeError:
raise InvalidEnvelopeError(f"Envelope must implement BaseEnvelope, envelope={envelope}")
except AttributeError as exc:
raise InvalidEnvelopeError(
f"Error: {str(exc)}. Please ensure that both the Input model and the Envelope inherits from BaseModel,\n" # noqa E501
"and your payload adheres to the specified Input model structure.\n"
f"Envelope={envelope}\nModel={model}",
)

try:
disable_pydantic_v2_warning()
Expand All @@ -181,5 +185,9 @@ def handler(event: Order, context: LambdaContext):
return model.parse_raw(event)

return model.parse_obj(event)
except AttributeError:
raise InvalidModelTypeError(f"Input model must implement BaseModel, model={model}")
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"
f"Model={model}",
)

0 comments on commit aacb37d

Please sign in to comment.