exception#

starlite.utils.exception.get_exception_handler(exception_handlers: ExceptionHandlersMap, exc: Exception) Optional[ExceptionHandler]#

Given a dictionary that maps exceptions and status codes to handler functions, and an exception, returns the appropriate handler if existing.

Status codes are given preference over exception type.

If no status code match exists, each class in the MRO of the exception type is checked and the first matching handler is returned.

Finally, if a 500 handler is registered, it will be returned for any exception that isn’t a subclass of HTTPException.

Parameters:
  • exception_handlers – Mapping of status codes and exception types to handlers.

  • exc – Exception Instance to be resolved to a handler.

Returns:

Optional exception handler callable.

class starlite.utils.exception.ExceptionResponseContent#

Bases: BaseModel

Represent the contents of an exception-response.

status_code: int#

Exception status code.

detail: str#

Exception details or message.

headers: Optional[Dict[str, str]]#

Headers to attach to the response.

extra: Optional[Union[Dict[str, Any], List[Any]]]#

An extra mapping to attach to the exception.

to_response() Response#

Create a response from the model attributes.

Returns:

A response instance.

starlite.utils.exception.create_exception_response(exc: Exception) Response#

Construct a response from an exception.

Notes

  • For instances of HTTPException or other exception classes that have a status_code attribute (e.g. Starlette exceptions), the status code is drawn from the exception, otherwise response status is HTTP_500_INTERNAL_SERVER_ERROR.

Parameters:

exc – An exception.

Returns:

HTTP response constructed from exception details.

Return type:

Response