A sample Python project
Project description
FastAPI Fancy Exceptions
Sample code
import uvicorn
from fastapi import FastAPI
from starlette import status
from starlette.requests import Request
from starlette.responses import JSONResponse
from src.fastapi_fancy_exceptions import FancyHTTPException
app = FastAPI()
class AuthException(FancyHTTPException):
code = "auth_error"
type = "AuthError"
message = "Auth error"
status_code = status.HTTP_401_UNAUTHORIZED
@app.exception_handler(FancyHTTPException)
async def http_exception_accept_handler(request: Request, exc: FancyHTTPException) -> JSONResponse:
return JSONResponse(
status_code=exc.status_code,
content=[{"code": exc.code, "type": exc.type, "message": exc.message}]
)
@app.get("/")
async def root():
raise AuthException()
uvicorn.run(app, host="localhost", port=8000)
This code will lead to this response with status code 401:
[
{
"code": "auth_error",
"type": "AuthError",
"message": "Auth error"
}
]
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 fastapi-fancy-exceptions-0.0.3.tar.gz
.
File metadata
- Download URL: fastapi-fancy-exceptions-0.0.3.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a60dcc0b14799532a29d669269aace2537910ac7b7bf786c654fb7a2c793bc0 |
|
MD5 | 50efc88fdd7e3d90be6e41ab4fb0cf3d |
|
BLAKE2b-256 | 6e01d596c34826c10ba8fdbf38c4b3ab0b25f6f439d0a985047d4d6ebb30b842 |
File details
Details for the file fastapi_fancy_exceptions-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: fastapi_fancy_exceptions-0.0.3-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 893ccfd6d9e7c0d8a9f176255c77d7e3f872fb9834583d44cb217fb3bc6bbbcd |
|
MD5 | 108caee2bcb59f6719486e7edde7ff8f |
|
BLAKE2b-256 | 79f160815c61348d9a450744763e47f0e720db6526c258a3ddfcd9ce2063a83b |