Skip to content

Commit bfbaad7

Browse files
committed
chore: highlight and wording
1 parent e9b7694 commit bfbaad7

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

docs/core/event_handler/api_gateway.md

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -481,39 +481,45 @@ Similarly to [Query strings](#query-strings-and-payload), you can access headers
481481

482482
### Handling not found routes
483483

484-
By default, Event Handler handles not found routes by simply returning 404 with `Not found` text.
485-
486-
If you'd like to handle it any differently, you can use `not_found` decorator.
487-
488-
```python
489-
from aws_lambda_powertools import Logger, Tracer
490-
from aws_lambda_powertools.logging import correlation_paths
491-
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver, Response
492-
from aws_lambda_powertools.event_handler.exceptions import NotFoundError
493-
494-
tracer = Tracer()
495-
logger = Logger()
496-
app = ApiGatewayResolver()
497-
498-
@app.not_found
499-
@tracer.capture_method
500-
def handle_not_found_errors(exc: NotFoundError) -> Response:
501-
# Return 418 upon 404 errors
502-
logger.info(f"Not found route: {app.current_event.path}")
503-
return Response(status_code=418, content_type=content_types.TEXT_PLAIN, body="I'm a teapot!")
504-
505-
506-
@app.get("/catch/me/if/you/can")
507-
@tracer.capture_method
508-
def catch_me_if_you_can():
509-
return {"message": "oh hey"}
510-
511-
# You can continue to use other utilities just as before
512-
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
513-
@tracer.capture_lambda_handler
514-
def lambda_handler(event, context):
515-
return app.resolve(event, context)
516-
```
484+
By default, we return `404` for any unmatched route.
485+
486+
You can use **`not_found`** decorator to override this behaviour, and return a custom **`Response`**.
487+
488+
=== "app.py"
489+
490+
```python hl_lines="10 12 15" title="Handling not found"
491+
from aws_lambda_powertools import Logger, Tracer
492+
from aws_lambda_powertools.logging import correlation_paths
493+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver, Response
494+
from aws_lambda_powertools.event_handler.exceptions import NotFoundError
495+
496+
tracer = Tracer()
497+
logger = Logger()
498+
app = ApiGatewayResolver()
499+
500+
@app.not_found
501+
@tracer.capture_method
502+
def handle_not_found_errors(exc: NotFoundError) -> Response:
503+
# Return 418 upon 404 errors
504+
logger.info(f"Not found route: {app.current_event.path}")
505+
return Response(
506+
status_code=418,
507+
content_type=content_types.TEXT_PLAIN,
508+
body="I'm a teapot!"
509+
)
510+
511+
512+
@app.get("/catch/me/if/you/can")
513+
@tracer.capture_method
514+
def catch_me_if_you_can():
515+
return {"message": "oh hey"}
516+
517+
# You can continue to use other utilities just as before
518+
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
519+
@tracer.capture_lambda_handler
520+
def lambda_handler(event, context):
521+
return app.resolve(event, context)
522+
```
517523

518524

519525
### Exception handling

0 commit comments

Comments
 (0)