Skip to main content

convert_exception_to_response

Wrap the given get_response callable in exception-to-response conversion.

All exceptions will be converted. All known 4xx exceptions (Http404, PermissionDenied, MultiPartParserError, SuspiciousOperation) will be converted to the appropriate response, and all other exceptions will be converted to 500 responses.

This decorator is automatically applied to all middleware to ensure that no middleware leaks an exception and that the next middleware in the stack can rely on getting a response instead of an exception.

def convert_exception_to_response(
get_response: callable
) - > callable

Wrap the given get_response callable in exception-to-response conversion. All exceptions will be converted. All known 4xx exceptions (Http404, PermissionDenied, MultiPartParserError, SuspiciousOperation) will be converted to the appropriate response, and all other exceptions will be converted to 500 responses. This decorator is automatically applied to all middleware to ensure that no middleware leaks an exception and that the next middleware in the stack can rely on getting a response instead of an exception.

Parameters

NameTypeDescription
get_responsecallableThe callable (often a middleware or view handler) that processes a request and returns a response.

Returns

TypeDescription
callableA wrapped version of the input callable that catches exceptions and returns an HTTP response object instead.