Skip to content

patera.exceptions

HTTP exceptions and exception handlers. See the exception handling guide.

Exceptions submodule

ExceptionHandler

ExceptionHandler(app: AppT)

Bases: Injectable, Generic[AppT]

get_exception_mapping

get_exception_mapping() -> dict[type[BaseException], Callable]

Maps each handled exception class to its handler method.

BaseHttpException

BaseHttpException(message: str = '', status_code: int | HttpStatus = 500, status='error', data=None)

Bases: Exception

Base http exception class

Init method

RequestValidationError

RequestValidationError(validation_error: ValidationError, message: str = 'Validation failed', status_code: int = 422, status: str = 'error')

Bases: BaseHttpException

Raised when @consumes cannot build the declared model from the request body. Answered with 422 Unprocessable Content; data (and errors) lists each problem's location, message and type. The submitted values are left out, so a rejected password is never sent back.

The original Pydantic error is validation_error. A ValidationError raised anywhere else (in handler code, or validating data from a database or another service) is a server error, answered with 500.

InvalidRequestBody

InvalidRequestBody(message: str = 'Malformed request body', status_code: int = 400, status: str = 'error', data: Any = None)

Bases: BaseHttpException

Raised when a request body cannot be parsed, such as malformed JSON. Answered with 400 Bad Request.

RequestBodyTooLarge

RequestBodyTooLarge(max_content_length: int, message: str = 'Request body too large', status_code: int = 413, status: str = 'error')

Bases: BaseHttpException

Raised when a request body is larger than the allowed maximum. Answered with 413 Content Too Large.

AborterException

AborterException(message: str = '', status_code: int = 400, status: str = 'error', data: Optional[Any] = None)

Bases: BaseHttpException

Aborter exception

StaticAssetNotFound

StaticAssetNotFound(message: str = 'Static asset not found', status_code: int = 404, status: str = 'error', data: Any = None)

Bases: BaseHttpException

HTTP exception for static assets not found

StaticPageNotFound

StaticPageNotFound(page: str, status_code: int = 404, status: str = 'error', data: Any = None)

Bases: BaseHttpException

Static page not found error

ClientDisconnected

Bases: CustomException

Raised when the client disconnects before its request body has arrived completely. The handler is stopped (and a transaction rolled back), and no response is sent, since nobody is left to receive it.

CustomException

Bases: Exception

Base custom exception class

DuplicateRouteError

DuplicateRouteError(message)

Bases: CustomException, ValueError

Raised when two different handlers are registered for the same HTTP method and path.

CircularDependencyError

CircularDependencyError(message)

Bases: CustomException, RuntimeError

Raised when injected dependencies require each other in a cycle.

MethodNotControllerMethod

MethodNotControllerMethod(message)

Bases: CustomException

Error if the decorated method is not part of a controller

UnexpectedDecorator

UnexpectedDecorator(message)

Bases: CustomException

Error for unexpected decorators

handles

handles(*exceptions: Type[Exception])

Decorator registers exceptions with handler method

abort

abort(msg: str, status_code: int | HttpStatus = BAD_REQUEST, status: str = 'error', data: Any = None)

Aborts request by raising an aborter exception

html_abort

html_abort(template: str, status_code: int | HttpStatus = BAD_REQUEST, data: Any = None)

Aborts request with html response