Skip to main content

FastAPI support for RFC9457 problems.

Project description

FastAPI Problems

image image image style tests codecov

fastapi_problem is a set of exceptions and handlers for use in fastapi applications to support easy error management and responses.

Each exception easily marshals to JSON based on the RFC9457 spec for use in api errors.

Check the docs for more details.

Custom Errors

Subclassing the convenience classes provide a simple way to consistently raise the same error with details/extras changing based on the raised context.

from fastapi_problem.error import NotFoundProblem


class UserNotFoundError(NotFoundProblem):
    title = "User not found."

raise UserNotFoundError(details="details")
{
    "type": "user-not-found",
    "title": "User not found",
    "details": "details",
    "status": 404,
}

Usage

import fastapi
from fastapi_problem.handler import add_exception_handler


app = fastapi.FastAPI()
add_exception_handler(app)

@app.get("/user")
async def get_user():
    raise UserNotFoundError("No user found.")
$ curl localhost:8000/user
{

    "type": "user-not-found",
    "title": "User not found",
    "details": "No user found.",
    "status": 404,
}

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_problem-0.8.0.tar.gz (9.0 kB view hashes)

Uploaded Source

Built Distribution

fastapi_problem-0.8.0-py3-none-any.whl (9.9 kB view hashes)

Uploaded Python 3

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