Shared base exception utilities for FastAPI apps.
Reason this release was yanked:
There are some changes which is specific to the requirements
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
- Use the most specific exception type that matches your error scenario
- For custom status codes or scenarios not covered by standard exceptions, use
GenericHttpError - Provide meaningful error details to help clients understand and resolve issues
- Use custom headers sparingly and only when they provide additional value
- 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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file abs_exception_core-0.2.3.tar.gz.
File metadata
- Download URL: abs_exception_core-0.2.3.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79886ad58d7dcccd26e5e13010e4f2798277b6dd59e22bd6a4be21fe9e182a80
|
|
| MD5 |
1e036be5c06e3378cffd9129c817e3a7
|
|
| BLAKE2b-256 |
3b3d79239fec31b34c6a1bff1528ca3117fc44e1f87fd884102b840ce17df02c
|
File details
Details for the file abs_exception_core-0.2.3-py3-none-any.whl.
File metadata
- Download URL: abs_exception_core-0.2.3-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16889454da4a0f0b92dbca498f06495390ead8a846cafcbaa3dff580c869c9e3
|
|
| MD5 |
600a3d4938c81998932d22fa2414051b
|
|
| BLAKE2b-256 |
017297c5f963644f3261ae850495b020320cdd6384d9e641a7d6814fe74cf8c3
|