common http exception that support custom status code and message
Project description
Awesome Exception
A library designed to handle http exception elegantly with customization support like i18n support.
Feature
- common http exception class that support custom message and status code
Usage
Installation
pip install awesome-exception
Exceptions
Using fast API as example, we may simply throw exception with a proper status code, and an optional error code. We may also supply arbitrary key value in args dict, to help frontend render better error message.
from awesome_exception.exceptions import NotFound
from fastapi import APIRouter
router = APIRouter()
@router.get('/transactions')
def get(id: str):
try:
obj = find_by_id(id)
except Exception as e:
raise NotFound(message='transaction not found' % id, error_code='A0001', args={id: id})
...
And we may implement a common error handler to convert all these errors to proper response schema
from awesome_exception.exceptions import HTTPException
from fastapi.requests import Request
from fastapi.responses import JSONResponse
@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
return JSONResponse(
status_code=exc.status_code,
content={
'detail': exc.detail,
'error_code': exc.error_code,
}
)
This would result in a response with status code 404, and body
{
"status_code": 404,
"detail": {
"message": "transaction not found",
"id": "some_id"
},
"error_code": "A0001"
}
With this response, frontend can decide to simply render detail, or map it to detailed message. If error_code "A0001" correspond to the following i18 n entry
"error.A0001": {"en-US": "transaction can not be found with supplied {id}: {message}"}
we may format message accordingly with
errorMessage = formatMessage({ id: `error.${error.data.error_code}` }, error.data.detail);
Note that error code is not supplied, is default to status code. So it is always safe to simply use error_code in frontend to decide what to render.
Development
Installing Poetry
- create your own environment for poetry, and simply run:
pip install poetry
- alternatively, you can refer to poetry's official page
- to be able to use
poe
directly,pip install poethepoet
Contributing
- project setup:
poetry install
- create your own branch to start developing new feature.
- before creating pr, make sure you pass
poe lint
andpoe test
.- what happened inside
poe test
is that a minio server is setup for you temporarily, and teardown and unit test is finished. - notice that
poe test
would also work if you already have a minio up and running. You need the following env variable:MINIO_ACCESS_KEY
,MINIO_SECRET_KEY
,MINIO_ADDRESS
upon runningpoe test
.
- what happened inside
- for a list of available poe command,
poe
- after you submit a pr, you should check if pipeline is successful.
Releasing
poetry version [new_version]
git commit -m"Bump version"
git push origin develop
- create new release on github.
- Create release off develop branch, auto generate notes, and review release note.
- Publish release
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 awesome-exception-1.1.0.tar.gz
.
File metadata
- Download URL: awesome-exception-1.1.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.8.13 Linux/5.15.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5c36a62e8c8b2a7b23ce574765759ecdaa5704e6ef5624e11f565d26a22198c |
|
MD5 | aa3468dcf23f357b844c3c3850bf2dbd |
|
BLAKE2b-256 | 823028aec6735354d3103c8c1ea2b4d1df0a6a6e7de00c0fd03e106fd97acd61 |
File details
Details for the file awesome_exception-1.1.0-py3-none-any.whl
.
File metadata
- Download URL: awesome_exception-1.1.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.11 CPython/3.8.13 Linux/5.15.0-1014-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 573b22de046e59b79966972a9e29d499176412a9c7c3afcb66a0105ca56520ec |
|
MD5 | f8c8250ee58efafe56524212f3e3d72b |
|
BLAKE2b-256 | 9612ce960c617c1a5697b1aa62d80497b688c6525aeca924682f22e585ba9129 |