flask_more_smorest.error

Error handling module for Flask-More-Smorest.

This module provides exception classes and error handlers for the application.

exception flask_more_smorest.error.ApiException(message=None, **kwargs)[source]

Base exception class for all API errors.

This exception class provides automatic error response generation following RFC 7807 Problem Details format, along with logging and debug information collection.

TITLE

Human-readable error title (default: “Error”)

MESSAGE_PREFIX

Prefix for error messages (default: “”)

HTTP_STATUS_CODE

HTTP status code for the error (default: 500)

INCLUDE_TRACEBACK

Whether to include traceback in response. Set to None to use environment-aware default (enabled in debug/testing). Set to True/False to override explicitly.

debug_context

Additional context information for debugging

Example

>>> class MyCustomError(ApiException):
...     TITLE = "Custom Error"
...     HTTP_STATUS_CODE = HTTPStatus.BAD_REQUEST
>>> raise MyCustomError("Something went wrong")
TITLE = 'Error'
MESSAGE_PREFIX = ''
HTTP_STATUS_CODE = 500
INCLUDE_TRACEBACK: bool | None = None
__init__(message=None, **kwargs)[source]

Initialize the API exception.

Parameters:
  • message (str | None) – Error message to display

  • **kwargs (str | int | bool | None) – Additional context information

property debug_context: dict[str, str | int | bool | dict | None]

Lazily compute debug context on first access.

This property defers the expensive operation of collecting user context until the debug context is actually needed (typically only in debug mode when generating error responses).

Returns:

Dictionary containing debug context including user information

classmethod error_code()[source]

Get the error code for this exception type.

Return type:

str

Returns:

Snake-case error code derived from class name

get_debug_context(**kwargs)[source]

Get debugging context information.

Collects user context via the configurable user context system, which works with both built-in and custom User models.

Parameters:

**kwargs (str | int | bool | None) – Additional context information to include

Return type:

dict[str, str | int | bool | dict | None]

Returns:

Dictionary containing debug context including user information (in debug mode)

make_error_response()[source]

Create an RFC 7807 Problem Details response.

Returns a response following the RFC 7807 format: - type: URI identifying the error type - title: Human-readable title - status: HTTP status code - detail: Human-readable explanation - instance: URI of the resource (if in request context)

In debug/testing mode, additional fields are included: - debug: Object containing traceback and context

Return type:

Response

Returns:

Flask Response object with problem details

log_exception()[source]

Log the exception with the appropriate level based on severity.

Return type:

None

exception flask_more_smorest.error.BadRequestError(message=None, **kwargs)[source]

400 Bad Request error.

TITLE = 'Bad Request'
HTTP_STATUS_CODE = 400
exception flask_more_smorest.error.DBError(message=None, **kwargs)[source]

Database error (500 status code).

TITLE = 'Database Error'
HTTP_STATUS_CODE = 500
exception flask_more_smorest.error.ForbiddenError(message=None, *, operation=None, resource_type=None, resource_id=None, reason=None, **kwargs)[source]

403 Forbidden error with permission context.

Provides detailed information about permission failures including the operation attempted, resource type, resource ID, and failure reason.

operation

Operation attempted (e.g., ‘modify’, ‘create’, ‘delete’)

resource_type

Type of resource (e.g., ‘Article’, ‘User’)

resource_id

ID of the specific resource

reason

Human-readable reason for denial

TITLE = 'Forbidden'
HTTP_STATUS_CODE = 403
__init__(message=None, *, operation=None, resource_type=None, resource_id=None, reason=None, **kwargs)[source]

Initialize ForbiddenError with permission context.

Parameters:
  • message (str | None) – Error message (auto-generated if not provided)

  • operation (str | None) – Operation attempted (e.g., ‘modify’, ‘create’, ‘delete’)

  • resource_type (str | None) – Type of resource (e.g., ‘Article’, ‘User’)

  • resource_id (Any) – ID of the specific resource

  • reason (str | None) – Human-readable reason for denial

  • **kwargs (str | int | bool | None) – Additional debug_context information

exception flask_more_smorest.error.InternalServerError(message=None, **kwargs)[source]

500 Internal Server Error.

TITLE = 'Internal Server Error'
HTTP_STATUS_CODE = 500
get_debug_context(**kwargs)[source]

Get debugging context including exception information.

Parameters:

**kwargs (str | int | bool | None) – Additional context information

Return type:

dict[str, str | int | bool | dict | None]

Returns:

Dictionary with base context plus exception details

exception flask_more_smorest.error.NotFoundError(message=None, **kwargs)[source]

404 Not Found error.

TITLE = 'Not Found'
HTTP_STATUS_CODE = 404
exception flask_more_smorest.error.UnauthorizedError(message=None, **kwargs)[source]

401 Unauthorized error.

TITLE = 'Unauthorized'
INCLUDE_TRACEBACK: bool | None = False
HTTP_STATUS_CODE = 401
exception flask_more_smorest.error.UnprocessableEntity(fields, location='json', message=None, valid_data=None, **kwargs)[source]

422 Unprocessable Entity error for validation failures.

This exception follows RFC 7807 with additional validation-specific fields: - errors: Object mapping locations to field errors

fields

Dictionary of field names to error messages

location

Where the validation failed (json, query, file, etc.)

valid_data

Data that passed validation (if any)

TITLE = 'Validation Error'
HTTP_STATUS_CODE = 422
__init__(fields, location='json', message=None, valid_data=None, **kwargs)[source]

Initialize the UnprocessableEntity exception.

Parameters:
  • fields (dict[str, str]) – Dictionary mapping field names to error messages

  • location (str) – Where the error occurred (default: “json”)

  • message (str | None) – Overall error message (default: “Invalid input data”)

  • valid_data (dict[str, str | int | bool] | None) – Data that passed validation

  • **kwargs (str | int | bool | None) – Additional debug_context information

fields: dict[str, str]
location: str | None
valid_data: dict[str, str | int | bool] | None
make_error_response()[source]

Create an RFC 7807 response with validation errors.

Extends the base Problem Details format with validation-specific fields: - errors: Object mapping location to field-level errors

Return type:

Response

Returns:

Flask Response object with validation error details

flask_more_smorest.error.handle_api_exception(e)[source]

Handle ApiException and its subclasses.

Parameters:

e (ApiException) – The API exception to handle

Return type:

Response

Returns:

Flask Response with error details

flask_more_smorest.error.handle_db_exception(e)[source]

Handle database exceptions.

Automatically rolls back the database session before generating the error response.

Parameters:

e (DatabaseError) – The database error to handle

Return type:

Response

Returns:

Flask Response with error details

flask_more_smorest.error.handle_generic_exception(e)[source]

Handle generic Python exceptions.

Parameters:

e (Exception) – The exception to handle

Return type:

Response

Returns:

Flask Response with error details or original HTTP response

flask_more_smorest.error.server_error_handler(e)[source]

Handle unhandled server errors.

Parameters:

e (Exception) – The exception that was raised

Return type:

Response

Returns:

Flask Response with error details

flask_more_smorest.error.unauthorized_handler(e, errors=None, level='info', warnings=None)[source]

Handle unauthorized access errors.

Parameters:
  • e (Exception) – The exception that was raised

  • errors (dict[str, str] | None) – Optional error details

  • level (str) – Logging level to use

  • warnings (list[str] | None) – Optional warning messages

Return type:

Response

Returns:

Flask Response with error details

Modules

error_handlers

Error handlers for Flask-More-Smorest.

exceptions

Exception classes for Flask-More-Smorest API errors.