Skip to main content

Tests Linting

fastapi-simple-errors

Simple error handling for fastapi using custom error classes.

Introduction

This small Python package aims to simplify error handling for FastAPI:

  • It allows defining custom exception classes in a simple way with little boilerplate code.
  • Your application functions can raise these errors and they will be propageted to FastAPI and result in a proper 4xx or 5xx status code to be sent to the client.
  • Proper OpenAPI documentation can be generated using a generic response schema for errors, which includes the error ID (Python class name) and a human-readable error message.

This package was inspired by the following discussions and projects:

Usage

The package fastapi-simple-errors is available on PyPi, so it can be installed with Python package managers such as pip or poetry.

First, you need to import the base errors from the package:

from fastapi_simple_errors import BadRequestError, NotFoundError, UnauthorizedError

Now, you can define your custom errors based on you application's needs:

class UserNotFoundError(NotFoundError):
    """The user was not found in the database."""


class InvalidUserIdError(BadRequestError):
    """The provided user ID is not valid."""


class InvalidTokenError(UnauthorizedError):
    """The provided authentication token is not valid."""

These error's inherit from FastAPI's HTTPException and will use the corresponding HTTP status codes, for example:

  • 400 for BadRequestError
  • 401 for UnauthorizedError
  • 404 for NotFoundError

In your FastAPI application, you could write:

@app.get("/users/{user_id}")
async def read_user(user_id: str):
    if user_id not in users:
        raise UserNotFoundError()
    return {"user": users[user_id]}

Note that this error could also be raised in sub-functions and will be propagated accordingly.

This will result in the following response:

HTTP/1.1 404 Not Found
...

{
    "detail": {
        "error": "UserNotFoundError",
        "message": "The user was not found in the database."
    }
}

Note that the error class name is returned in the response as error identifier and the error message is used from the docstring. However, you could overwrite the message and also set custom headers:

raise UserNotFoundError("A custom message", headers={"X-State": "..."})

The predefined errors (like NotFoundError) all inherit from AppError. This can be used to define your own errors with custom status codes, e.g.:

from from fastapi_simple_errors import AppError
from fastapi import status

class TeapotError(AppError):
    """This server is a teapot."""

    status_code = status.HTTP_418_IM_A_TEAPOT

To include the errors (and error response schema) in the OpenAPI documentation, you can use the package's error_responses function, like this:

from fastapi_simple_errors import BadRequestError, NotFoundError, error_responses

@app.get("/users/{user_id}", responses=error_responses(BadRequestError, NotFoundError))
async def read_user(user_id: str):
    if is_invalid(user_id):
        raise InvalidUserIdError()
    if user_id not in users:
        raise UserNotFoundError()
    return {"user": users[user_id]}

For more concise code, you could alternatively use the error_responses_from_status_codes function:

from fastapi_simple_errors import BadRequestError, NotFoundError
from fastapi_simple_errors import error_responses_from_status_codes as err

@app.get("/users/{user_id}", responses=err(400, 404))
async def read_user(user_id: str):
    if is_invalid(user_id):
        raise InvalidUserIdError()
    if user_id not in users:
        raise UserNotFoundError()
    return {"user": users[user_id]}

The generated OpenAPI documentation will look like this (using Redoc):

openapi

Licence

MIT

Metadata

Release files for fastapi-simple-errors 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-simple-errors 1.0.0
File Size Uploaded
fastapi_simple_errors-1.0.0.tar.gz 5.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-simple-errors 1.0.0
File Interpreter ABI Platform
fastapi_simple_errors-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.0 kB

Release files / fastapi_simple_errors-1.0.0.tar.gz

Download URL fastapi_simple_errors-1.0.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c14f6117d2fb68c62775ae0af060228f439e20148f4aec608f4cceaee0a5f8ee
BLAKE2b-256 checksum
How to use checksums
ab9abeff67f1d2004edea041078ce3836632740d592d6b055794627b078ef845
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.9 Darwin/23.6.0

Release files / fastapi_simple_errors-1.0.0-py3-none-any.whl

Download URL fastapi_simple_errors-1.0.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23371d9c21fa31897254845ab32196f1ff229df5a790748ff23c40b2692e8516
BLAKE2b-256 checksum
How to use checksums
14c692a3d6590ecf6c44267cd1a3dc9367942336ccec8521502988441da54278
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.9 Darwin/23.6.0

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release 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