Thin wrapper for layabase and layab error handling
Project description
Exceptions handling for layab
This module allows to throw exceptions in your code and send a proper HTTP response automatically to your client.
Depending on the REST Framework you use, your need to add exception handler(s) to your API.
Starlette
Register exceptions handlers
If you want to document your API about those specific return types and the expected body, you can use layab to create your Starlette application.
You can also register all provided exceptions at once yourself using layaberr.starlette.exception_handlers
from starlette.applications import Starlette
import layaberr.starlette
app = Starlette(exception_handlers=layaberr.starlette.exception_handlers)
Supported Exceptions
The following exceptions are available
ValidationFailed
In case your endpoint raises ValidationFailed, an HTTP error 400 (Bad Request) will be sent to the client.
Error not specific to a field in received data
This code:
from layaberr.starlette import ValidationFailed
received_data = None
raise ValidationFailed(received_data, message="This is the error message")
Will result in the following JSON response sent to the client:
{"fields": [{"item": 1, "field_name": "", "messages": ["This is the error message"]}]}
Error specific to a field in a received dictionary
This code:
from layaberr.starlette import ValidationFailed
received_data = {"field 1": "value 1"}
raise ValidationFailed(received_data, errors={"field 1": ["Invalid value"]})
Will result in the following JSON response sent to the client:
{"fields": [{"item": 1, "field_name": "field 1", "messages": ["Invalid value"]}]}
Error specific to a field in a received list of dictionaries
This code:
from layaberr.starlette import ValidationFailed
received_data = [{"field 1": "value 1"}, {"field 1": "value 2"}]
raise ValidationFailed(received_data, errors={1: {"field 1": ["Invalid value"]}})
Will result in the following JSON response sent to the client:
{"fields": [{"item": 2, "field_name": "field 1", "messages": ["Invalid value"]}]}
Unauthorized
In case your endpoint raises Unauthorized, an HTTP error 401 (Unauthorized) will be sent to the client.
This code:
from layaberr.starlette import Unauthorized
raise Unauthorized("The exception message")
Will result in the following JSON response sent to the client:
{"message": "The exception message"}
Forbidden
In case your endpoint raises Forbidden, an HTTP error 403 (Forbidden) will be sent to the client.
This code:
from layaberr.starlette import Forbidden
raise Forbidden("The exception message")
Will result in the following JSON response sent to the client:
{"message": "The exception message"}
Exception (this is the default handler)
In case your endpoint raises an Exception, an HTTP error 500 (Internal Server Error) will be sent to the client.
This code:
raise Exception("The exception message")
Will result in the following JSON response sent to the client:
{"message": "The exception message"}
How to install
- python 3.6+ must be installed
- Use pip to install module:
python -m pip install layaberr
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
Hashes for layaberr-3.0.0.dev0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9608e73d98047a18f2ab975d23e0e88b262e8662809fcee4973ddbcb6e7b1a47 |
|
MD5 | c4c2c9e7f00e0d4236bcd359fc34df51 |
|
BLAKE2b-256 | 72e7755f918ff4b71a2f0120c76922789c4818355e7f6ee7d38919c10f06cfce |