apifrom.middleware.error_handling

Error handling middleware for APIFromAnything.

This module provides middleware for catching and formatting exceptions in a consistent way for API responses.

Overview

Classes

Classes

ErrorHandlingMiddleware(debug = False, include_traceback = False, include_exception_class = False, log_exceptions = True, json_encoder = None, **kwargs):bases: apifrom.middleware.base.Middleware

Middleware for handling errors in API requests.

This middleware catches exceptions and returns appropriate error responses.

Initialize the error handling middleware.

param debug:

Whether to include debug information in error responses

param include_traceback:

Whether to include tracebacks in debug mode

param include_exception_class:

Whether to include exception class names

param log_exceptions:

Whether to log exceptions

param json_encoder:

A custom JSON encoder for error responses

param **kwargs:

Additional options for the base middleware

apifrom.middleware.error_handling._create_error_response(message, status_code, error_code, exception, details=None)

Create an error response.

param message:

The error message

param status_code:

The HTTP status code

param error_code:

The error code

param exception:

The original exception

param details:

Additional error details

returns:

An API response

apifrom.middleware.error_handling._find_handler(exception)

Find the appropriate handler for an exception.

param exception:

The exception to handle

returns:

A handler function

apifrom.middleware.error_handling._handle_api_error(exception)

Handle an API error.

param exception:

The API error

returns:

An API response

apifrom.middleware.error_handling._setup_default_handlers()

Set up default exception handlers.

apifrom.middleware.error_handling.add_exception_handler(exception_class, handler)

Add a custom exception handler.

param exception_class:

The exception class to handle

param handler:

A function that converts the exception to a response

apifrom.middleware.error_handling.dispatch(request, call_next)
:async:

Dispatch a request, catching and handling any exceptions.

param request:

The request to process

param call_next:

The next middleware or route handler

returns:

The response

apifrom.middleware.error_handling.process_request(request)
:async:

Process a request.

param request:

The request to process

returns:

The processed request

class apifrom.middleware.error_handling.ExceptionHandler(exception_class, status_code, error_code)[source]
Parameters:

Handler for converting exceptions to API responses.

Initialize an exception handler.

param exception_class:

The exception class to handle

param status_code:

The HTTP status code to return

param error_code:

The error code to include in the response

apifrom.middleware.error_handling.__call__(exception)

Convert an exception to a response.

param exception:

The exception to handle

returns:

An API response

Middleware(app, dispatch = None):bases: starlette.middleware.base.BaseHTTPMiddleware, BaseMiddleware

Middleware class for APIFromAnything.

This class implements the BaseMiddleware interface and extends Starlette’s BaseHTTPMiddleware to provide a middleware component that can be used with Starlette.

Initialize a new BaseMiddleware instance.

param **options:

Options for the middleware.

class apifrom.middleware.error_handling.Request(request=None, path_params=None, method=None, path=None, query_params=None, headers=None, body=None, client_ip=None)[source]
Parameters:
  • request (Optional[starlette.requests.Request])

  • path_params (Optional[dict[Any, Any]])

  • method (Optional[str])

  • path (Optional[str])

  • query_params (Optional[dict[Any, Any]])

  • headers (Optional[dict[Any, Any]])

  • body (Optional[Union[str, bytes]])

  • client_ip (Optional[str])

Request class for APIFromAnything.

This class wraps a Starlette request and provides methods for accessing request data in a convenient way.

apifrom.middleware.error_handling._request

The underlying Starlette request.

apifrom.middleware.error_handling.path_params

Path parameters extracted from the URL.

apifrom.middleware.error_handling.query_params

Query parameters extracted from the URL.

apifrom.middleware.error_handling.headers

HTTP headers.

apifrom.middleware.error_handling.method

HTTP method.

apifrom.middleware.error_handling.path

Request path.

apifrom.middleware.error_handling._body

Cached request body.

Initialize a new Request instance.

param request:

The underlying Starlette request.

param path_params:

Path parameters extracted from the URL.

param method:

The HTTP method.

param path:

The request path.

param query_params:

Query parameters.

param headers:

HTTP headers.

param body:

Request body.

param client_ip:

Client IP address.

class apifrom.middleware.error_handling.Response(content=None, status_code=200, headers=None, content_type='application/json')[source]
Parameters:
  • content (Any)

  • status_code (int)

  • headers (Optional[Dict[str, str]])

  • content_type (str)

Response class for APIFromAnything.

This class represents an HTTP response and provides methods for setting response data, status code, and headers.

apifrom.middleware.error_handling.content

The response content.

apifrom.middleware.error_handling.status_code

The HTTP status code.

apifrom.middleware.error_handling.headers

HTTP headers.

apifrom.middleware.error_handling.content_type

The content type of the response.

Initialize a new Response instance.

param content:

The response content.

param status_code:

The HTTP status code.

param headers:

HTTP headers.

param content_type:

The content type of the response.