From aacb37ddcd221db75c2cb53497cd603945c4bc93 Mon Sep 17 00:00:00 2001 From: Leandro Damascena Date: Fri, 5 Jan 2024 12:33:22 +0000 Subject: [PATCH] refactor(parser): Improve error message when parsing models and envelopes (#3587) * Adding more verbose to Parser error message * Addressing Ruben's feedback --- aws_lambda_powertools/utilities/parser/parser.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/aws_lambda_powertools/utilities/parser/parser.py b/aws_lambda_powertools/utilities/parser/parser.py index 9d3f8afa2c8..91d2763f4ec 100644 --- a/aws_lambda_powertools/utilities/parser/parser.py +++ b/aws_lambda_powertools/utilities/parser/parser.py @@ -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() @@ -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}", + )