Skip to main content

Python Fastapi's general exception handler

Project description

FastAPI Router Handler

FastAPI Router Handler is a package designed to help you manage exception handling in FastAPI applications in a clean and consistent way. This package allows you to handle exceptions gracefully in router handlers while maintaining a consistent response format.

Key Features

  • Asynchronous exception handling support
  • Custom logging integration
  • Consistent error response format
  • Global exception handling through middleware
  • Customizable error messages

Installation

pip install fastapi-router-handler

Usage

Basic Example

from fastapi import FastAPI
from fastapi_router_handler import ExceptionHandler

app = FastAPI()
exception_handler = ExceptionHandler()

@app.get("/example")
async def example_endpoint():
    return await exception_handler.exception_handler(
        lambda: {"message": "success"}
    )

@app.get("/error-example")
async def error_endpoint():
    return await exception_handler.exception_handler(
        lambda: raise_error(),  # Function that raises an error
        e_code=400,
        e_msg="Custom error message"
    )

Using Custom Logger

import logging
from fastapi_router_handler import ExceptionHandler

logger = logging.getLogger(__name__)
exception_handler = ExceptionHandler(logger=logger)

Response Examples

Success Response

{
  "message": "success"
}

Error Response

{
    "detail": "Internal Server Error",  # Requires e_msg setting, default: "Internal Server Error"
    "status_code": 500  # Requires e_code setting, default: 500
}

License

MIT License


Contributing

Contributions are welcome! Please feel free to submit a pull request.

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_router_handler-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_router_handler-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_router_handler-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_router_handler-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.1 Darwin/24.1.0

File hashes

Hashes for fastapi_router_handler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c53ae98e2e3aaad07fe71eb7b000ba244aaec0e95b8a5af091ec44ced20a91b3
MD5 4d9e37ef21231b8cea2138f1dc311a34
BLAKE2b-256 ca13209cb70b1e862de667187053e9a3176abfce9a5147436b9a54d62a7ba3a2

See more details on using hashes here.

File details

Details for the file fastapi_router_handler-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_router_handler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52247410ce6231859602ce4f4059d41d0d66c9bea05ab21f8e5b082eb5fc9fad
MD5 f51050d4fbfacb9b45b2aeb1d0aef7c9
BLAKE2b-256 0402268e02c8436c673220ce1d8d0869166140015bad295b159e595f4476eb3a

See more details on using hashes here.

Supported by

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