Skip to main content

Package for useful fastapi utils

Project description

FastAPI Helper

Simple and customizable HTTP exceptions

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-helper-0.0.11.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

fastapi_helper-0.0.11-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-helper-0.0.11.tar.gz.

File metadata

  • Download URL: fastapi-helper-0.0.11.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for fastapi-helper-0.0.11.tar.gz
Algorithm Hash digest
SHA256 5422c454b4d3564c31c95d344dbc16b97e830489b3434d86cb9d4867a93a2a43
MD5 82200eb9d218d53c5dff96680f68b85d
BLAKE2b-256 8b6682abdad627d0bc21119410b9ea5d4b1c0c0abc9a95b358c5dcbff7ff6c51

See more details on using hashes here.

File details

Details for the file fastapi_helper-0.0.11-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_helper-0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 a040c006c00138f937d518edebefc3ff6b25b47e0203b3fd9704f78807d46572
MD5 465b6a35c475cb1668c07f1b3579d063
BLAKE2b-256 c07e31e6c00d94d8ad6147d8b2a04f1d77979f5665b118b292aaaef7db98c276

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