Skip to content
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

feat(event_handler): define exception_handler directly from the router #3979

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #1193

Summary

Changes

This pull request introduces a new feature allowing the direct definition of an exception_handler from the router. This enhancement is particularly beneficial for customers who are segmenting their routers into multiple files and including them in the Resolver.

User experience

split_route_module.py

from aws_lambda_powertools import Tracer
from aws_lambda_powertools.event_handler.api_gateway import Router, Response

tracer = Tracer()
router = Router()

endpoint = "https://jsonplaceholder.typicode.com/todos"


@router.get("/hello")
def get_todos():
    raise ValueError("ERROR")

@router.exception_handler(ValueError)
def handle_invalid_limit_qs(ex: ValueError):  # receives exception raised

    return Response(
        status_code=400,
        body="Invalid request parameters.",
    )

app.py

import split_route_module

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver()


app.include_router(split_route_module.router)  

# You can continue to use other utilities just as before
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)

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.

@leandrodamascena leandrodamascena requested a review from a team as a code owner March 18, 2024 21:36
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 18, 2024
Copy link

sonarcloud bot commented Mar 18, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

@github-actions github-actions bot added the feature New feature or functionality label Mar 18, 2024
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 72.72727% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 96.30%. Comparing base (e14e768) to head (3ed9443).
Report is 195 commits behind head on develop.

Files Patch % Lines
aws_lambda_powertools/event_handler/api_gateway.py 72.72% 2 Missing and 1 partial ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3979      +/-   ##
===========================================
- Coverage    96.38%   96.30%   -0.08%     
===========================================
  Files          214      215       +1     
  Lines        10030    10262     +232     
  Branches      1846     1915      +69     
===========================================
+ Hits          9667     9883     +216     
- Misses         259      271      +12     
- Partials       104      108       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@leandrodamascena leandrodamascena changed the title feat(event_handler): define exception_handler directly from the router. feat(event_handler): define exception_handler directly from the router Mar 19, 2024
@rubenfonseca
Copy link
Contributor

Reviewing now

Copy link
Contributor

@rubenfonseca rubenfonseca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and clean, loved it!

@leandrodamascena leandrodamascena merged commit a403f4d into aws-powertools:develop Mar 19, 2024
23 checks passed
@leandrodamascena leandrodamascena deleted the merge-exception-handler branch March 19, 2024 09:14
@alecrevangelista
Copy link

Cool! @leandrodamascena // @rubenfonseca which release version will this be a part of?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers feature New feature or functionality size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: app.exception_handler does not work when used in separate file
4 participants