mirror of
https://github.com/d3vyce/fastapi-toolsets.git
synced 2026-03-02 01:10:47 +01:00
doc: add missing docstring + add missing feature to README (#57)
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
"""Standardized API exceptions and error response handlers."""
|
||||
|
||||
from .exceptions import (
|
||||
ApiError,
|
||||
ApiException,
|
||||
|
||||
@@ -87,6 +87,12 @@ class InsufficientRolesError(ForbiddenError):
|
||||
)
|
||||
|
||||
def __init__(self, required_roles: list[str], user_roles: set[str] | None = None):
|
||||
"""Initialize the exception.
|
||||
|
||||
Args:
|
||||
required_roles: Roles needed to access the resource
|
||||
user_roles: Roles the current user has, if known
|
||||
"""
|
||||
self.required_roles = required_roles
|
||||
self.user_roles = user_roles
|
||||
|
||||
@@ -130,6 +136,11 @@ class NoSearchableFieldsError(ApiException):
|
||||
)
|
||||
|
||||
def __init__(self, model: type) -> None:
|
||||
"""Initialize the exception.
|
||||
|
||||
Args:
|
||||
model: The SQLAlchemy model class that has no searchable fields
|
||||
"""
|
||||
self.model = model
|
||||
detail = (
|
||||
f"No searchable fields found for model '{model.__name__}'. "
|
||||
|
||||
@@ -12,6 +12,25 @@ from .exceptions import ApiException
|
||||
|
||||
|
||||
def init_exceptions_handlers(app: FastAPI) -> FastAPI:
|
||||
"""Register exception handlers and custom OpenAPI schema on a FastAPI app.
|
||||
|
||||
Installs handlers for :class:`ApiException`, validation errors, and
|
||||
unhandled exceptions, and replaces the default 422 schema with a
|
||||
consistent error format.
|
||||
|
||||
Args:
|
||||
app: FastAPI application instance
|
||||
|
||||
Returns:
|
||||
The same FastAPI instance (for chaining)
|
||||
|
||||
Example:
|
||||
from fastapi import FastAPI
|
||||
from fastapi_toolsets.exceptions import init_exceptions_handlers
|
||||
|
||||
app = FastAPI()
|
||||
init_exceptions_handlers(app)
|
||||
"""
|
||||
_register_exception_handlers(app)
|
||||
app.openapi = lambda: _custom_openapi(app) # type: ignore[method-assign]
|
||||
return app
|
||||
|
||||
Reference in New Issue
Block a user