Skip to main content

Verbose HTTP exceptions

PyPI version coverage

types - Mypy License - MIT code style CI actions status Release actions status

For what?

I made this package to make my work with http exceptions more easier. In FastAPI I had problem with HTTP exceptions - they are too simple. Only detail field and that is all. And other tools that make http exceptions more verbose works not like I expect.

This package was inspired by drf-exceptions-hog, but implemented for other Web-frameworks.

Install

To install the package you need you run the following commands.

For pip:

pip install verbose_http_exceptions

For poetry:

poetry add verbose_http_exceptions

For PDM:

pdm add verbose_http_exceptions

Usage

You can use all these exceptions for your need even without any web-framework, but mostly, it may be useless, so use extensions in this package or write your own, if you need.

Then all (or some specific part of) your exceptions will be returned to users in JSON like this:

{
    "code": "validation_error",
    "type": "literal_error",
    "message": "Input should be 1 or 2",
    "attr": "a",
    "location": "query",
}

or this (multiple exceptions supported too):

{
    "code": "multiple",
    "type": "multiple",
    "message": "Multiple exceptions ocurred. Please check list for details.",
    "attr": null,
    "location": null,
    "nested_errors": [
        {
            "code": "validation_error",
            "type": "literal_error",
            "message": "Input should be 1 or 2",
            "attr": "a",
            "location": "query",
        },
        {
            "code": "validation_error",
            "type": "missing",
            "message": "Field required",
            "attr": "b",
            "location": "query",
        }
    ]
}

FastAPI implementation

To work with this utility you must add exception handlers in your FastAPI project like this:

from fastapi import FastAPI
from verbose_http_exceptions.ext.fastapi import (
    apply_verbose_http_exception_handler,
    apply_all_handlers,
)

app = FastAPI()
apply_all_handlers(app)
# or
apply_verbose_http_exception_handler(app)
# See document-strings of functions for more information.

[!NOTE] Specific use Package contains appliers, which add handlers to FastAPI instance, and handlers itself, so you can work with them directly. Import them from regular package path or pass .handlers to it.

[!TIP] apply_all_handler function also has override_422_openapi param (default True). You can turn it off to avoid overriding 422 errors in your application OpenAPI schema.

Litestar implementation

To work with this utility you must add exception handlers in your Litestar project like this:

from litestar import Litestar
from verbose_http_exceptions.ext.litestar import ALL_EXCEPTION_HANDLERS_MAP

app = Litestar(
    exception_handlers=ALL_EXCEPTION_HANDLERS_MAP
)

[!NOTE] Specific use ALL_EXCEPTION_HANDLERS_MAP is a ready to use dictionary with all exception handlers. Extension has other handlers and handler mappings, so you can import them directly with Litestar instance.

[!WARNING] Possible incorrect use Make sure, you pass handlers and handler mappings correctly, because they are not general, so algorithms inside them can be different, and if you pass, for example, python_error_handler with litestar ValidationException, server will always return 500 internal server error without any context, if there is validation request error raised.

What is next?

I like this project, and I want to implement it for many web-frameworks and add new functionality, so my goals are to:

  • Integrate this package with litestar.
  • Add OpenAPI override for Litestar. FastAPI already has override functionality to add to 422 errors verbose schema and description.
  • Add all http-exceptions for all status codes.
  • Add status codes module to make work with my package easier.
  • Add tests for all exceptions (Now only specific errors tested for coverage).
  • Add extra mapping to response (Litestar compatibility + good idea itself), but pass only important context.
  • Add other content response types like XML.

Release files for verbose_http_exceptions 3.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 verbose_http_exceptions 3.0.0
File Size Uploaded
verbose_http_exceptions-3.0.0.tar.gz 32.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for verbose_http_exceptions 3.0.0
File Interpreter ABI Platform
verbose_http_exceptions-3.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 69.8 kB

Release files / verbose_http_exceptions-3.0.0.tar.gz

Download URL verbose_http_exceptions-3.0.0.tar.gz
Size 32.6 kB
Tags Source
SHA-256 checksum
How to use checksums
24b20add2abc46d1d27282cdddf320df495226c76551b75b8241fb027992f1a5
BLAKE2b-256 checksum
How to use checksums
a858cb2882ca287923304e78f5d4ad0b9c0aa4be213a46455d1c043c53bc9ea3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.18.0 CPython/3.11.9 Linux/6.5.0-1025-azure

Release files / verbose_http_exceptions-3.0.0-py3-none-any.whl

Download URL verbose_http_exceptions-3.0.0-py3-none-any.whl
Size 37.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ede55e293fe4def21c2ec79aa832274232e785fdc6ae38c0386fd22ace327235
BLAKE2b-256 checksum
How to use checksums
e0ace319182a8b1bef0ea8ea4aa5b71b30c43853c766f3d478917766f842561c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.18.0 CPython/3.11.9 Linux/6.5.0-1025-azure

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.0.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

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