[PYTHON] About FastAPI ~ Endpoint error handling ~

FastAPI error handling sample

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

About FastAPI ~ Endpoint error handling ~
About tweepy error handling
Mainframe error handling
Python Error Handling
SikuliX error handling
django.db.migrations.exceptions.InconsistentMigrationHistory error handling
Python, about exception handling
Error handling in PythonBox
GraphQL (gqlgen) error handling
Around feedparser error handling
About Go error handling * This is a rough article.
Error handling when installing mecab-python
Data handling 3 (development) About data format
PyCUDA build error handling memorandum
About handling Django static files
Error divided by 0 Handling of ZeroDivisionError
[Error handling] peewee.IntegrityError 1451 occurs in peewee
Error handling when updating Fish shell
About import error of PyQt5.QtWidgets (Anaconda)