Skip to main content

Shared base exception utilities for FastAPI apps.

Project description

Exception Core Package

A comprehensive exception handling package for FastAPI applications that provides standardized error responses and exception handling.

Features

  • Pre-defined HTTP exceptions for common error scenarios
  • Dynamic error creation for custom scenarios
  • Standardized error response format
  • Request validation error handling

Installation

pip install abs-exception-core

Usage

1. Import and Register Exception Handlers

from fastapi import FastAPI
from abs_exception_core.exception_handlers import (
    request_validation_exception_handler,
    global_exception_handler,
    # ... other handlers
)

app = FastAPI()

# Register exception handlers
app.add_exception_handler(RequestValidationError, request_validation_exception_handler)
app.add_exception_handler(Exception, global_exception_handler)

2. Using Pre-defined Exceptions

from abs_exception_core.exceptions import (
    NotFoundError,
    ValidationError,
    AuthError,
    GenericHttpError,
    # ... other exceptions
)

# Example usage in a route
@app.get("/items/{item_id}")
async def get_item(item_id: int):
    if not item_exists(item_id):
        raise NotFoundError(detail="Item not found")
    
    if not has_permission():
        raise AuthError(detail="Insufficient permissions")

3. Using Generic Error

The GenericHttpError class allows you to create custom HTTP exceptions with any status code:

# Create a custom error with status code 418
error = GenericHttpError(
    status_code=418,
    detail="I'm a teapot",
    headers={"X-Custom-Header": "value"}
)

# Create a custom error with just status code and message
error = GenericHttpError(
    status_code=451,
    detail="Unavailable For Legal Reasons"
)

# Create a custom error with just status code
error = GenericHttpError(status_code=402)

Available Exceptions

  • DuplicatedError (409 Conflict)
  • AuthError (403 Forbidden)
  • NotFoundError (404 Not Found)
  • ValidationError (422 Unprocessable Entity)
  • PermissionDeniedError (403 Forbidden)
  • UnauthorizedError (401 Unauthorized)
  • BadRequestError (400 Bad Request)
  • ConflictError (409 Conflict)
  • InternalServerError (500 Internal Server Error)
  • RateLimitExceededError (429 Too Many Requests)
  • ServiceUnavailableError (503 Service Unavailable)
  • GenericHttpError (Custom Status Code) - For creating custom HTTP exceptions

Error Response Format

All errors follow a standardized format:

{
    "message": "Error message",
    "error": "Detailed error description",
    "type": "ErrorType",
    "details": {
        "path": "/api/endpoint",
        "method": "GET"
    },
    "errors": [
        {
            "field": "field_name",
            "message": "Validation message",
            "type": "error_type",
            "input_value": "invalid_value"
        }
    ]
}

Best Practices

  1. Use the most specific exception type that matches your error scenario
  2. For custom status codes or scenarios not covered by standard exceptions, use GenericHttpError
  3. Provide meaningful error details to help clients understand and resolve issues
  4. Use custom headers sparingly and only when they provide additional value
  5. Follow the standardized error response format for consistency

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

abs_exception_core-0.2.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

abs_exception_core-0.2.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file abs_exception_core-0.2.0.tar.gz.

File metadata

  • Download URL: abs_exception_core-0.2.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.9 Darwin/23.6.0

File hashes

Hashes for abs_exception_core-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2a7a75f86bd4d9f883aff6f1f7173420da1732c5ab8a927f423ccb0a4f6314c4
MD5 1cd102605e42ee4c4f58dbb602ea90dc
BLAKE2b-256 efa16d2490421c288a67bbabf2cad71fb9c5b4bdf7708498eaa6084ffc847ac7

See more details on using hashes here.

File details

Details for the file abs_exception_core-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: abs_exception_core-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.9 Darwin/23.6.0

File hashes

Hashes for abs_exception_core-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 458fb91ee682ce8dd4f4445346e42c5829b1b95b9b5c8e535a7ccb99c659621f
MD5 48f587e233028dbfe7218611f92a1ceb
BLAKE2b-256 e555bdbcc6bd30384f5fe10e7ee626ffffb07e209eaa49e7ad28658dd41e01e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page