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
ErrorHandlingMiddlewareMiddleware
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]ο
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]ο
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]ο
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.