Skip to content

Exception handler

Drop-in DRF exception handler that renders every error as a uniform envelope. See the Exception handler guide for the full code list, customisation hooks, and edge cases.

exception_handler

exception_handler(
    exc: Exception, context: dict[str, Any]
) -> Response | None

Render every exception as restflow's error envelope.

Used via REST_FRAMEWORK = {"EXCEPTION_HANDLER": "restflow.exceptions.exception_handler"}. Maps DRF, Django, and restflow.exceptions.APIException instances onto the error response {error: {code, message, details}} and falls through to DRF's default for anything else.

APIException

APIException(
    detail: Any = None,
    *,
    code: str | ErrorCode | None = None,
    details: dict[str, Any] | None = None,
    status_code: int | None = None,
)

Bases: APIException

DRF APIException carrying a stable error code and structured details.

Subclass and override code, default_detail, status_code to expose application-specific errors that render through restflow.exceptions.exception_handler as a uniform formatter.

ErrorCode

Bases: str, Enum

Stable error codes returned in restflow's error response.

format_error

format_error(
    code: str | ErrorCode,
    message: str,
    details: dict[str, Any] | None = None,
) -> dict[str, Any]

Build the canonical error envelope dict.