Bug: validate() raises raw jmespath ParseError instead of InvalidEnvelopeExpressionError for most malformed envelopes

Aberta Para iniciantes
#8,918 2 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

@0x5t4l1n já está trabalhando nisso.

Desde 2/12/2026.

  • #8943 de @0x5t4l1n — aberto

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
88/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
python
Domínio
backend

Direção de pesquisa

Comece em jmespath_utils.query(), onde, segundo a issue, LexerError, TypeError e UnicodeError são capturados. Verifique como o validator converte erros de consulta e revise os testes funcionais em tests/functional/validator. Adicione casos para os envelopes malformados listados e confirme que eles geram InvalidEnvelopeExpressionError e que o comportamento da função personalizada continua coberto.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

bug triage
Expected Behaviour

validate() and the @validator decorator document InvalidEnvelopeExpressionError "when JMESPath expression to unwrap event is invalid", and the exception documents itself as raised "When JMESPath fails to parse expression". So any envelope with a JMESPath syntax error should raise InvalidEnvelopeExpressionError.

Current Behaviour

Only a tokenizer error (LexerError, e.g. an unclosed quote) is converted. Every other syntax error escapes as jmespath's own exception, so except InvalidEnvelopeExpressionError doesn't catch it:

envelope raised
powertools_json(data jmespath.exceptions.IncompleteExpressionError
powertools_json(data, data) jmespath.exceptions.ArityError
data. jmespath.exceptions.ParseError
powertools_json("data) InvalidEnvelopeExpressionError (documented)

jmespath_utils.query() catches (LexerError, TypeError, UnicodeError). LexerError is one subclass of ParseError; IncompleteExpressionError and ArityError are its siblings.

Code snippet
import jmespath.exceptions
from aws_lambda_powertools.utilities.validation import InvalidEnvelopeExpressionError, validate

schema = {"type": "object", "properties": {"message": {"type": "string"}}, "required": ["message"]}
event = {"data": '{"message": "hello"}'}

for envelope in ["powertools_json(data", "powertools_json(data, data)", "data.", 'powertools_json("data)']:
    try:
        validate(event=event, schema=schema, envelope=envelope)
    except InvalidEnvelopeExpressionError:
        print(f"{envelope!r}: InvalidEnvelopeExpressionError")
    except jmespath.exceptions.JMESPathError as e:
        print(f"{envelope!r}: raw {type(e).__name__}")
Possible Solution

Catch jmespath.exceptions.ParseError (the base class of LexerError, IncompleteExpressionError and ArityError) instead of LexerError in jmespath_utils.query(). One line, plus a parametrized functional test in tests/functional/validator. The new cases fail on develop and pass with the change.

I left UnknownFunctionError alone: test_custom_jmespath_function_overrides_builtin_functions pins that it surfaces as-is when custom functions replace the built-ins.

I have this ready on a branch and can open a PR once this is triaged: https://github.com/khaledix3/powertools-lambda-python/compare/develop...fix/jmespath-invalid-expression-error

One compatibility note: code that catches jmespath.exceptions.ParseError around validate() for these cases would need to catch InvalidEnvelopeExpressionError instead, which is what the docs already say to catch. Happy to follow whatever you prefer under ADR 0006.

Steps to Reproduce
  1. Run the snippet above on develop (0bbc252, version 3.41.0).
  2. Only powertools_json("data) prints InvalidEnvelopeExpressionError; the other three print a raw jmespath exception.
Did you run the reproduction yourself?

Yes, I ran it and saw the failure

Powertools for AWS Lambda (Python) version

3.41.0 (develop @ 0bbc252)

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs
'powertools_json(data': raw IncompleteExpressionError
'powertools_json(data, data)': raw ArityError
'data.': raw ParseError
'powertools_json("data)': InvalidEnvelopeExpressionError
Linguagem predominante
Python
Estrelas
3.3k
Forks
511
Merge médio
2d 23h
PRs com merge (30d)
50

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de aws-powertools/powertools-lambda-python

Todas as issues de aws-powertools/powertools-lambda-python

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.