Skip to main content

Verbose HTTP exceptions package for web frameworks.

Project description

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.

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

verbose_http_exceptions-3.0.0.tar.gz (32.6 kB view details)

Uploaded Source

Built Distribution

verbose_http_exceptions-3.0.0-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file verbose_http_exceptions-3.0.0.tar.gz.

File metadata

  • Download URL: verbose_http_exceptions-3.0.0.tar.gz
  • Upload date:
  • Size: 32.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.18.0 CPython/3.11.9 Linux/6.5.0-1025-azure

File hashes

Hashes for verbose_http_exceptions-3.0.0.tar.gz
Algorithm Hash digest
SHA256 24b20add2abc46d1d27282cdddf320df495226c76551b75b8241fb027992f1a5
MD5 25141160ff509ce6626ea5ba5e682e80
BLAKE2b-256 a858cb2882ca287923304e78f5d4ad0b9c0aa4be213a46455d1c043c53bc9ea3

See more details on using hashes here.

File details

Details for the file verbose_http_exceptions-3.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for verbose_http_exceptions-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ede55e293fe4def21c2ec79aa832274232e785fdc6ae38c0386fd22ace327235
MD5 1761b4f998ded328a7abf3188b64c2d1
BLAKE2b-256 e0ace319182a8b1bef0ea8ea4aa5b71b30c43853c766f3d478917766f842561c

See more details on using hashes here.

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