aws-powertools / powertools-lambda-python

A developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/python/latest/
MIT No Attribution
2.73k stars 378 forks source link

fix(event_handler): do not skip middleware and exception handlers on 404 error #4492

Closed heitorlessa closed 1 month ago

heitorlessa commented 1 month ago

Issue number: #3916

Summary

This PR addresses the bug where non-matched routes (404) did not kick off the request chain and instead immediately process a 404 response.

The reasons this PR do not reach for a 404 middleware solution are:

  1. Middlewares are tightly coupled with a Route, so request processing is kicked off from a route (__call__())
  2. Moving as a middleware would require a dummy/no-op route to be called only to be overwritten, causing unnecessary CPU cycle and memory usage (tbh, it's somewhat a minor and we can revert later).

NOTE. Nothing stops us for making it a middleware w/ a dummy route to kick the request chain when we have more time. This was an issue ever since middleware was introduced in #2917, so I'd favoured keeping the core the same and focusing on covering any edge cases (e.g., what if a middleware short-circuit).

Changes

Please provide a summary of what's being changed

unrelated

User experience

Please share what the user experience looks like before and after this change

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change? **RFC issue number**: Checklist: * [ ] Migration process documented * [ ] Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

codecov[bot] commented 1 month ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 96.43%. Comparing base (e14e768) to head (395f1ad). Report is 563 commits behind head on develop.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #4492 +/- ## =========================================== + Coverage 96.38% 96.43% +0.05% =========================================== Files 214 219 +5 Lines 10030 10627 +597 Branches 1846 1976 +130 =========================================== + Hits 9667 10248 +581 - Misses 259 267 +8 - Partials 104 112 +8 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

heitorlessa commented 1 month ago

@leandrodamascena can I get your help in doing your crazy QA skills just in case I missed any edge case?

sonarcloud[bot] commented 1 month ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
2.3% Duplication on New Code

See analysis details on SonarCloud

leandrodamascena commented 1 month ago

@leandrodamascena can I get your help in doing your crazy QA skills just in case I missed any edge case?

Hey @heitorlessa, I've spent some time testing various scenarios with different kinds of Resolvers, including Bedrock, and everything seems to be working fine for me. CORS is working correctly also, and it's also running Middleware with the OPTIONS method/route.

e2e tests are also green. A note for the future is to create e2e tests with Middleware to ensure that we are testing those scenarios.

image