Skip to content

refactor(parser): Improve error message when parsing models and envelopes #3587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}",
)