Web based error utils
Project description
Web Errors v0.6.9
web_error
is a set of exceptions and handlers for use in starlette/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.
Migrating to 0.6 (RFC9457 support)
Check the discussion here for details on how to update usage to maintain legacy functionality or move over to problem details support.
Errors
The base web_error.error.HttpException
accepts a title
, details
, status
(default 500) and optional **kwargs
. An additional code
can be passed in,
which will be used as the type
, if not provided the type
is derived from
the class name.
And will return a JSON response with exc.status
as the status code and response body:
{
"type": "an-exception",
"title": "title",
"details": "details",
"status": 500,
"extra-key": "extra-value",
...
}
Derived types are generated using the class name after dropping ...Error
from
the end, and converting to kebab-case
. i.e. PascalCaseError
will derive the
type pascal-case
. If the class name doesn't suit your purposes, an optional
code
attribute can be set with the desired value of there response type
field.
Some convenience Exceptions are provided with predefined status
attributes.
To create custom errors subclasss these and define the title
attribute.
web_error.error.ServerException
provides status 500 errorsweb_error.error.BadRequestException
provides status 400 errorsweb_error.error.UnauthorisedException
provides status 401 errorsweb_error.error.NotFoundException
provides status 404 errors
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 web_error.error import NotFoundException
class UserNotFoundError(NotFoundException):
title = "User not found."
raise UserNotFoundError(details="details")
{
"type": "user-not-found",
"title": "User not found",
"details": "details",
"status": 404,
}
Whereas a defined code
will be used in the output.
class UserNotFoundError(NotFoundException):
title = "User not found."
code = "cant-find-user"
raise UserNotFoundError(details="details")
{
"type": "cant-find-user",
"title": "User not found",
"details": "details",
"status": 404,
}
If additional kwargs are provided when the error is raised, they will be included in the output (ensure the provided values are json seriablizable.
raise UserNotFoundError(details="details", user_id="1234", metadata={"hello": "world"})
{
...
"details": "details",
"user_id": "1234",
"metadata": {"hello": "world"},
}
Starlette
import starlette.applications
import web_error.handler.starlette
app = starlette.applications.Starlette()
web_error.handler.starlette.add_exception_handler(app)
A custom logger can be provided to add_exception_handler(app, logger=...)
.
If you require cors headers, you can pass a web_error.cors.CorsConfiguration
instance to add_exception_handler(cors=...)
.
add_exception_handler(
app,
cors=CorsConfiguration(
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
allow_credentials=True,
)
)
To handle unexpected errors provide unhandled_wrappers
, a dict mapping http
status code to HttpCodeException
, the system key default
is also accepted
as the root wrapper for all unhandled exceptions.
If you wish to hide debug messaging from external users, strip_debug=True
will log the debug message and remove it from the response.
from web_error.error import HttpCodeException
class NotFoundError(HttpCodeException):
status = 404
message = "Endpoint not found."
web_error.handler.starlette.add_exception_handler(
app,
unhandled_wrappers={
"404": NotFoundError,
},
)
FastAPI
The FastAPI handler is identical to the starlette handler with the additional
handling of RequestValidationError
.
import fastapi
import web_error.handler.fastapi
app = fastapi.FastAPI()
web_error.handler.fastapi.add_exception_handler(app)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file web_error-0.6.9.tar.gz
.
File metadata
- Download URL: web_error-0.6.9.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.9.18 Linux/6.5.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bf0f374ad87a35d27ab977e76ec7a5c9b377fb62bf43c1875bda39c166b8cc88 |
|
MD5 | c4c8ee6b45cff777cdc2c9fccf255c33 |
|
BLAKE2b-256 | 050c5952c6bcc23e22eea99f57f90c648d92210ff8eebd3b571185b178634884 |
Provenance
File details
Details for the file web_error-0.6.9-py3-none-any.whl
.
File metadata
- Download URL: web_error-0.6.9-py3-none-any.whl
- Upload date:
- Size: 13.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.9.18 Linux/6.5.0-1015-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 639cf9b3213fdde5288e7eab1d130d944b06b9960affe77254b5567c0e404a56 |
|
MD5 | 4d6740edaac0426dbd32329e9af96e56 |
|
BLAKE2b-256 | c50e40c06b73b7f2138ae5e32faaae3be61ee6351e2912439a6f1befd25c53c5 |