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. .. py:currentmodule:: apifrom.middleware.error_handling Overview -------- **Classes** * :py:class:`ErrorHandlingMiddleware` * :py:class:`ExceptionHandler` * :py:class:`Middleware` * :py:class:`Request` * :py:class:`Response` Classes ------- .. py:class:: 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 .. :: debug .. :: exception_handlers .. :: include_exception_class .. :: include_traceback .. :: json_encoder .. :: log_exceptions .. method:: _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 .. method:: _find_handler(exception) Find the appropriate handler for an exception. :param exception: The exception to handle :returns: A handler function .. method:: _handle_api_error(exception) Handle an API error. :param exception: The API error :returns: An API response .. method:: _setup_default_handlers() Set up default exception handlers. .. method:: 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 .. method:: 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 .. method:: process_request(request) :async: Process a request. :param request: The request to process :returns: The processed request .. py:class:: ExceptionHandler(exception_class, status_code, error_code) 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 .. :: error_code .. :: exception_class .. :: status_code .. method:: __call__(exception) Convert an exception to a response. :param exception: The exception to handle :returns: An API response .. py:class:: 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. .. py:class:: Request(request = None, path_params = None, method = None, path = None, query_params = None, headers = None, body = None, client_ip = None) Request class for APIFromAnything. This class wraps a Starlette request and provides methods for accessing request data in a convenient way. .. attribute:: _request The underlying Starlette request. .. attribute:: path_params Path parameters extracted from the URL. .. attribute:: query_params Query parameters extracted from the URL. .. attribute:: headers HTTP headers. .. attribute:: method HTTP method. .. attribute:: path Request path. .. attribute:: _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. .. py:class:: Response(content = None, status_code = 200, headers = None, content_type = 'application/json') Response class for APIFromAnything. This class represents an HTTP response and provides methods for setting response data, status code, and headers. .. attribute:: content The response content. .. attribute:: status_code The HTTP status code. .. attribute:: headers HTTP headers. .. attribute:: 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.