-
Notifications
You must be signed in to change notification settings - Fork 5
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
feature(apigateway): define function to handle not found requests #65
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hey @rtcnerd, thanks for raising this with us. Routes are exact matches or regex patterns. This means, that print statement will only execute if an incoming request matches that route "/not-found-error". There is an issue/PR to add support for handling 404s and the likes. If there's a particular UX you'd like us to provide please do let us know! |
@heitorlessa what is the best way to handle 404 event and send back a custom message? |
Within your Lambda Handler, inspect the response from "app.resolve(event, context)" and see if "statusCode" is 404. By default, docs suggest to simply "return app.resolve(event, context)". At least until we provide a more streamlined way like "@app.not_found" and "@app.exception_handler()" |
🤔 hmmm, too bad there isn't a way to override the serializer in ApiGatewayResolver for errors edit: this is what I will do for now: try:
response = app.resolve(event, context)
except Exception:
response = {
"statusCode": 500,
"message": {
"type": "https://datatracker.ietf.org/doc/html/rfc7807",
"title": "Internal Server Error",
"detail": "An unexpected error has occurred",
"status": 500,
},
}
if response["statusCode"] >= 400:
return Response(
status_code=response["statusCode"],
content_type="application/problem+json",
body=response["message"],
) |
There’s a WIP PR for it for the next release, Jake ;-)
…On Tue, 14 Dec 2021 at 21:44, Jake Brinkmann ***@***.***> wrote:
🤔 hmmm, too bad there isn't a way to override the serializer in
ApiGatewayResolver for errors
https://github.com/awslabs/aws-lambda-powertools-python/blob/be6e722e3d552c56ef916c87dffeb40e394fe499/aws_lambda_powertools/event_handler/api_gateway.py#L612-L617
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/awslabs/aws-lambda-powertools-python/issues/884#issuecomment-993974406>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBFMCQUUVCFNAZ6E6YTUQ6UD5ANCNFSM5JZDORAQ>
.
|
EDIT: Correct syntax error on Done - it'll be available as part of today's release. Both not found and any exception you want to handle app = ApiGatewayResolver()
logger = Logger()
@app.exception_handler(SomeKindOfError)
def handle_error(ex: SomeKindOfError):
print(f"request path is '{app.current_event.path}'")
return Response(status_code=418, content_type=content_types.TEXT_HTML, body=str(ex))
@app.not_found
def handle_not_found(exc: NotFoundError):
return Response(status_code=404, content_type=content_types.TEXT_PLAIN, body="I am a teapot!")
@app.exception_handler(ServiceError)
def service_error(ex: ServiceError):
logger.debug(f"Log out sensitive stuff: {ex.msg}")
return Response(
status_code=ex.status_code,
content_type=content_types.APPLICATION_JSON,
body="CUSTOM ERROR FORMAT",
)
@app.get("/my/path")
def call_with_error() -> Response:
raise SomeKindOfError("Foo!")
@app.get("/my/path2")
def call_with_error_sensitive() -> Response:
raise InternalServiceError("Foo!")
def lambda_handler(event, context):
return app(event, context) |
hey @rtcnerd this is now available as part of today's 1.23.0 release :) Thank you again for this feature request! |
@heitorlessa many thanks for introducing the new feature. However when I try executing your sample code on aws, i get the following error for not found: |
🤦♂️ there's a syntax error in my comment up there - () - decorator should be @app.not_found not @app.not_found() or else you'll get the error you've got, my apologies. The official docs are correct Let me know if you're still experiencing the same issue with the correct sample |
@heitorlessa - should i accept |
@heitorlessa @rtcnerd - sorry about the errors with |
@rtcnerd - anything else missing from this? |
Executing the following code does not trigger the rules. Ideally I would expect the rule="/not-found-error" to be triggered when an incorrect path is sent. The print statement is never printed. I get the default value. Am I missing something?
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
from aws_lambda_powertools.event_handler.exceptions import (
BadRequestError,
InternalServerError,
NotFoundError,
ServiceError,
UnauthorizedError,
)
tracer = Tracer()
logger = Logger()
app = ApiGatewayResolver()
@app.get(rule="/bad-request-error")
def bad_request_error():
# HTTP 400
raise BadRequestError("Missing required parameter")
@app.get(rule="/unauthorized-error")
def unauthorized_error():
# HTTP 401
raise UnauthorizedError("Unauthorized")
@app.get(rule="/not-found-error")
def not_found_error():
# HTTP 404
print("Not found")
raise NotFoundError
@app.get(rule="/internal-server-error")
def internal_server_error():
# HTTP 500
raise InternalServerError("Internal server error")
@app.get(rule="/service-error", cors=True)
def service_error():
raise ServiceError(502, "Something went wrong!")
# alternatively
# from http import HTTPStatus
# raise ServiceError(HTTPStatus.BAD_GATEWAY.value, "Something went wrong)
def handler(event, context):
return app.resolve(event, context)
The text was updated successfully, but these errors were encountered: