异常与异常处理

exception litestar.exceptions.ClientException

基类:HTTPException

Client error.

status_code: int = 400

Exception status code.

exception litestar.exceptions.DTOFactoryException

基类:LitestarException

Base DTO exception type.

exception litestar.exceptions.HTTPException

基类:LitestarException

Base exception for HTTP error responses.

These exceptions carry information to construct an HTTP response.

__init__(*args: Any, detail: str = '', status_code: int | None = None, headers: dict[str, str] | None = None, extra: dict[str, Any] | list[Any] | None = None) None

Initialize HTTPException.

Set detail and args if not provided.

参数:
  • *args -- if detail kwarg not provided, first arg should be error detail.

  • detail -- Exception details or message. Will default to args[0] if not provided.

  • status_code -- Exception HTTP status code.

  • headers -- Headers to set on the response.

  • extra -- An extra mapping to attach to the exception.

status_code: int = 500

Exception status code.

extra: dict[str, Any] | list[Any] | None

An extra mapping to attach to the exception.

headers: dict[str, str] | None

Headers to attach to the response.

detail: str

Exception details or message.

exception litestar.exceptions.ImproperlyConfiguredException

基类:HTTPException, ValueError

Application has improper configuration.

exception litestar.exceptions.InternalServerException

基类:HTTPException

Server encountered an unexpected condition that prevented it from fulfilling the request.

status_code: int = 500

Exception status code.

exception litestar.exceptions.InvalidAnnotationException

基类:DTOFactoryException

Unexpected DTO type argument.

exception litestar.exceptions.LitestarException

基类:Exception

Base exception class from which all Litestar exceptions inherit.

__init__(*args: Any, detail: str = '') None

Initialize LitestarException.

参数:
  • *args -- args are converted to str before passing to Exception

  • detail -- detail of the exception.

exception litestar.exceptions.LitestarWarning

基类:UserWarning

Base class for Litestar warnings

exception litestar.exceptions.MethodNotAllowedException

基类:ClientException

Server knows the request method, but the target resource doesn't support this method.

status_code: int = 405

Exception status code.

exception litestar.exceptions.MissingDependencyException

基类:LitestarException, ImportError

Missing optional dependency.

This exception is raised only when a module depends on a dependency that has not been installed.

__init__(package: str, install_package: str | None = None, extra: str | None = None) None

Initialize LitestarException.

参数:
  • *args -- args are converted to str before passing to Exception

  • detail -- detail of the exception.

exception litestar.exceptions.NoRouteMatchFoundException

基类:InternalServerException

A route with the given name could not be found.

exception litestar.exceptions.NotAuthorizedException

基类:ClientException

Request lacks valid authentication credentials for the requested resource.

status_code: int = 401

Exception status code.

exception litestar.exceptions.NotFoundException

基类:ClientException, ValueError

Cannot find the requested resource.

status_code: int = 404

Exception status code.

exception litestar.exceptions.PermissionDeniedException

基类:ClientException

Request understood, but not authorized.

status_code: int = 403

Exception status code.

exception litestar.exceptions.SerializationException

基类:LitestarException

Encoding or decoding of an object failed.

exception litestar.exceptions.ServiceUnavailableException

基类:InternalServerException

Server is not ready to handle the request.

status_code: int = 503

Exception status code.

exception litestar.exceptions.TemplateNotFoundException

基类:InternalServerException

Referenced template could not be found.

__init__(*args: Any, template_name: str) None

Initialize TemplateNotFoundException.

参数:
  • *args (Any) -- Passed through to super().__init__() - should not include detail.

  • template_name (str) -- Name of template that could not be found.

exception litestar.exceptions.TooManyRequestsException

基类:ClientException

Request limits have been exceeded.

status_code: int = 429

Exception status code.

exception litestar.exceptions.ValidationException

基类:ClientException, ValueError

Client data validation error.

exception litestar.exceptions.WebSocketDisconnect

基类:WebSocketException

Exception class for websocket disconnect events.

__init__(*args: Any, detail: str, code: int = 1000) None

Initialize WebSocketDisconnect.

参数:
  • *args -- Any exception args.

  • detail -- Exception details.

  • code -- Exception code. Should be a number in the >= 1000.

exception litestar.exceptions.WebSocketException

基类:LitestarException

Exception class for websocket related events.

__init__(*args: Any, detail: str, code: int = 4500) None

Initialize WebSocketException.

参数:
  • *args -- Any exception args.

  • detail -- Exception details.

  • code -- Exception code. Should be a number in the >= 1000.

code: int

Exception code. For custom exceptions, this should be a number in the 4000+ range. Other codes can be found in litestar.status_code with the WS_ prefix.

class litestar.exceptions.responses.ExceptionResponseContent

基类:object

表示异常响应的内容。

status_code: int

Exception status code.

detail: str

Exception details or message.

media_type: MediaType | str

Media type of the response.

headers: dict[str, str] | None = None

Headers to attach to the response.

__init__(status_code: int, detail: str, media_type: MediaType | str, headers: dict[str, str] | None = None, extra: dict[str, Any] | list[Any] | None = None) None
extra: dict[str, Any] | list[Any] | None = None

An extra mapping to attach to the exception.

to_response(request: Request | None = None) Response

从模型属性创建响应。 Returns: 一个响应实例。

litestar.exceptions.responses.create_debug_response(request: Request, exc: Exception) Response

从异常创建调试响应。 Args: request: 触发异常的请求。 exc: 一个异常。 Returns: Response: 根据异常详情构建的调试响应。

litestar.exceptions.responses.create_exception_response(request: Request[Any, Any, Any], exc: Exception) Response

从异常构建响应。 Notes: - 对于 HTTPException 的实例或其他具有 status_code 属性(例如 Starlette 异常)的异常类,状态码从异常中获取,否则响应状态为 HTTP_500_INTERNAL_SERVER_ERROR。 Args: request: 触发异常的请求。 exc: 一个异常。 Returns: Response: 根据异常详情构建的 HTTP 响应。