Skip to main content

af-fastapi-exceptions

Reusable exception handling for FastAPI apps: a base ServiceError hierarchy you can raise (and extend), plus a middleware + validation handler so that every error — raised before, during, or after the route — reaches the client as the same JSON body.

Bring your own response model. The library never defines or imposes a response schema — you pass your own model down, and the handlers use only the slice they need (construct it with message + errors, then call .model_dump()). Your app keeps one model for both success and error responses; nothing is coupled across the boundary.

Installation

pip install af-fastapi-exceptions
# or with Poetry:
poetry add af-fastapi-exceptions

Raising errors

Raise a ServiceError (or a subclass) anywhere; the middleware turns it into your response model with the right status code.

from allfly.fastapi.exceptions import EntityNotFoundError, ForbiddenError

def get_user(user_id: str):
    user = repo.find(user_id)
    if not user:
        raise EntityNotFoundError(f"No user {user_id}")   # -> 404
    if not user.active:
        raise ForbiddenError()                            # -> 403
    return user

Built-in classes: UnauthorizedRequestError (401), ForbiddenError (403), BadRequestError (400), EntityNotFoundError (404), ResourceConflictError (409), UnprocessableRequestError (422), UnsupportedFeatureError (501), DownstreamServiceError (502).

ServiceWarning is a ServiceError subclass for expected/recoverable conditions — logged at warning level instead of error. ForbiddenError and UnsupportedFeatureError are warnings.

Extend them

from allfly.fastapi.exceptions import UnprocessableRequestError

class FopError(UnprocessableRequestError):
    def __init__(self, message="Payment method was rejected."):
        super().__init__(message)

Your response model

Provide any model whose instances have a .model_dump() and that can be constructed with message= and errors= (a pydantic model with those two fields — plus whatever else you want, e.g. timestamp, metadata — is the common case). The library only ever sets message and errors; the rest come from your model's defaults. This is the ApiErrorBody protocol:

class ApiErrorBody(Protocol):
    def __init__(self, *, message: str, errors: list[str]) -> None: ...
    def model_dump(self, *, mode: str = "json", exclude_none: bool = True) -> dict: ...

Wiring it into your app

from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from allfly.fastapi.exceptions import (
    ExceptionMiddleware,
    DefaultExceptionHandlerSettings,
    build_validation_handler,
    build_error_responses,
)
from myapp.models import ApiMessage   # <-- YOUR model

app = FastAPI(responses=build_error_responses(ApiMessage))   # OpenAPI error schemas

# Catches ServiceError (-> its status) and any unexpected Exception (-> 500).
app.add_middleware(
    ExceptionMiddleware,
    error_model=ApiMessage,
    settings=DefaultExceptionHandlerSettings(production=is_production()),
)

# RequestValidationError is raised during request parsing, before the middleware runs,
# so register it as an exception handler too — same body, built from your model.
app.add_exception_handler(RequestValidationError, build_validation_handler(ApiMessage))

Settings (why not read your env directly?)

The middleware only needs to know one thing: are we in production? (In production the 500 handler hides the raw exception string; otherwise it includes it to aid debugging.)

Rather than force a settings system on you, it takes any object matching the ExceptionHandlerSettings protocol — a single production: bool. Use the provided DefaultExceptionHandlerSettings, or pass your own object exposing production.

Logging

Handlers log via loguruServiceWarning at warning, real errors at error/exception. If your app doesn't configure loguru, the messages are simply not emitted at higher levels by default.

Download files

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

Source Distribution

af_fastapi_exceptions-0.0.5.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

af_fastapi_exceptions-0.0.5-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file af_fastapi_exceptions-0.0.5.tar.gz.

File metadata

  • Download URL: af_fastapi_exceptions-0.0.5.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for af_fastapi_exceptions-0.0.5.tar.gz
Algorithm Hash digest
SHA256 131a17d8b370f5c36769d380da0764899184adc382d8c02404108575b4e95428
MD5 fb1962cc8000be9d810a3241af0f0446
BLAKE2b-256 0e5d94b0bbed3624ac8cf0eb42723c385829799a084b26530c2bbbf14a31e819

See more details on using hashes here.

Provenance

The following attestation bundles were made for af_fastapi_exceptions-0.0.5.tar.gz:

Publisher: af-fastapi-exceptions-publish.yml on travelallfly/allfly-py-libs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file af_fastapi_exceptions-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for af_fastapi_exceptions-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 abe0d9beee513e1bc2a35ef636555be53905b685c8148e63c9c2188fa66e516a
MD5 9f22a5f83242cf3ead4d196b48bf4b3b
BLAKE2b-256 52f1fcd27da132d57167c9802c10b0323b69a390f235138e593a5b6b71b2826b

See more details on using hashes here.

Provenance

The following attestation bundles were made for af_fastapi_exceptions-0.0.5-py3-none-any.whl:

Publisher: af-fastapi-exceptions-publish.yml on travelallfly/allfly-py-libs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.0.6

2 files

This release

0.0.5 This release

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page