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.4.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

fastapi_helper-0.0.4-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi-helper-0.0.4.tar.gz
  • Upload date:
  • Size: 6.5 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.4.tar.gz
Algorithm Hash digest
SHA256 f34566ce55410a261520c7d46cda1525a1f603825b5ac51838922563dea106ba
MD5 9098f1a1e9444b10a91b0550f7581e46
BLAKE2b-256 95ad1042be850e07d5a5d4f626fd450371cdc6179a9a2e7399bc8427cc1fb329

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_helper-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3c0b406fabd548800ad5de8c530aa7857c849a961d65da90d50a11f8d9b3b6f0
MD5 a92484c503f46dc2ff716e69f7e02745
BLAKE2b-256 76bbd9d795a98f42468933f280fd20053d95849e26e5524ea3b21ca51c07db2f

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