Skip to content

Commit e9b7694

Browse files
committed
docs: add new not_found feature
1 parent b144f75 commit e9b7694

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/core/event_handler/api_gateway.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,48 @@ Similarly to [Query strings](#query-strings-and-payload), you can access headers
478478
return app.resolve(event, context)
479479
```
480480

481+
482+
### Handling not found routes
483+
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+
```
517+
518+
519+
### Exception handling
520+
521+
522+
481523
### Raising HTTP errors
482524

483525
You can easily raise any HTTP Error back to the client using `ServiceError` exception.

0 commit comments

Comments
 (0)