Skip to main content

Standardize and handle exceptions in FastAPI more elegantly

Project description

Routes Manager for FastAPI

Standardize and handle exceptions in FastAPI more elegantly.

Installation

$ pip install fastapi-exceptionshandler

Example

You can automatically handle all exceptions using fastapi-routesmanager. Simply register the APIExceptionManager to the RouteManagersRegistry. Don't forget to use the ManagedAPIRouter

Note: by default Validation errors are not captured. To do so, use APIExceptionManager(capture_validation=True) instead.

from fastapi import FastAPI

from fastapi_routesmanager import RouteManagersRegistry, ManagedAPIRouter

from fastapi_exceptionshandler import APIExceptionManager

RouteManagersRegistry.register_route_manager(APIExceptionManager)  # Register manager

app = FastAPI()

router = ManagedAPIRouter()

@router.get("/")  # Use router instead of app
def read_root():
    return 1/0


app.include_router(router)  # Include the router to the app
Or you can handle exceptions manually...
from fastapi import FastAPI

from fastapi_exceptionshandler import APIExceptionHandler

app = FastAPI()

@app.get("/")
def read_root():
    try:
        return 1/0
    except Exception as exc:
        return await APIExceptionHandler.unhandled(exc)

Create a custom exception class and error code, then

Run it

$ uvicorn main:app --reload

Check it

Browse to http://127.0.0.1:8000 you should see this json:

{"errorCode": "InternalError", "message": "Internal Server Error"}

Creating custom exceptions

In order to create a custom exception you need to extend APIException and create an Enum class.

Note: if you want to capture only APIException then use APIExceptionManager(capture_unhandled=False)

from enum import Enum

from fastapi import FastAPI

from fastapi_routesmanager import RouteManagersRegistry, ManagedAPIRouter

from fastapi_exceptionshandler import APIExceptionManager, APIException

RouteManagersRegistry.register_route_manager(APIExceptionManager)  # Register manager

app = FastAPI()

router = ManagedAPIRouter()


class CustomException(APIException):
    status_code = 401
    
    class ErrorCode(Enum):
        CustomExceptionCode = "Custom Exception Message"


@router.get("/")  # Use router instead of app
def read_root():
    raise CustomException(CustomException.ErrorCode.CustomExceptionCode)


app.include_router(router)  # Include the router to the app

Then you should get a 401 response with this body

{"errorCode": "CustomExceptionCode", "message": "Custom Exception Message"}
Or you can handle exceptions manually...
@app.get("/")
def read_root():
    try:
        raise CustomException(CustomException.ErrorCode.CustomExceptionCode)
    except APIException as exc:
        return await APIExceptionHandler.handled(exc)
    except Exception as exc:  # Handle all exceptions
        return await APIExceptionHandler.unhandled(exc)

Note: The ErrorCode class doesn't need to be inside CustomException and can be shared with another exceptions as well.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_exceptionshandler-0.0.2.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page