FastAPI version_0.61.1 Documentation: https://fastapi.tiangolo.com Source Code: https://github.com/tiangolo/fastapi
FastAPI endpoint can be implemented as follows Error handling becomes possible.
sample.py
from fastapi import routing
def handle_wrapper(func):
    def routing_wrapper(*args, **kwargs):
        routing_api = func(*args, **kwargs)
        async def api_handle(request: Request) -> Response:
            try:
                http_response = await routing_api(request)
            except Exception as ex:
                """Describe arbitrary exception handling here"""
            return http_response
        return api_handle
    return routing_wrapper
routing.get_request_handler = handle_wrapper(routing.get_request_handler)
http_response = await routing_api(request)
Since the endpoint is called on the 7th line ↑ of this code, It is also possible to define a callback function by applying the above code.
Recommended Posts