problem details¶
Plugin for converting exceptions into a problem details response.
- exception litestar.plugins.problem_details.ProblemDetailsException[源代码]¶
-
A problem details exception as per RFC 9457.
- __init__(*args: Any, detail: str = '', status_code: int | None = None, headers: dict[str, str] | None = None, extra: dict[str, Any] | list[Any] | None = None, type_: str | None = None, title: str | None = None, instance: str | None = None) None[源代码]¶
Initialize
ProblemDetailsException.- 参数:
*args¶ -- if
detailkwarg 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.
type_¶ -- The type field in the problem details.
title¶ -- The title field in the problem details.
instance¶ -- The instance field in the problem details.
- class litestar.plugins.problem_details.ProblemDetailsConfig[源代码]¶
基类:
objectThe configuration object for
ProblemDetailsPlugin.- exception_handler(exc: ProblemDetailsException) Response[Any]¶
The exception handler used for
ProblemdetailsException.
- enable_for_all_http_exceptions: bool = False¶
Flag indicating whether to convert all
HTTPExceptionintoProblemDetailsException.
- exception_to_problem_detail_map: ExceptionToProblemDetailMapType¶
A mapping to convert exceptions into
ProblemDetailsException.All exceptions provided in this will get a custom exception handler where these exceptions are converted into
ProblemDetailExceptionbefore handling them. This can be used to override the handler forHTTPExceptionas well.
- class litestar.plugins.problem_details.ProblemDetailsPlugin[源代码]¶
基类:
InitPluginA plugin to convert exceptions into problem details as per RFC 9457.
- __init__(config: ProblemDetailsConfig | None = None)[源代码]¶
- on_app_init(app_config: AppConfig) AppConfig[源代码]¶
Receive the
AppConfiginstance after on_app_init hooks have been called.示例
from litestar import Litestar, get from litestar.di import Provide from litestar.plugins import InitPluginProtocol def get_name() -> str: return "world" @get("/my-path") def my_route_handler(name: str) -> dict[str, str]: return {"hello": name} class MyPlugin(InitPluginProtocol): def on_app_init(self, app_config: AppConfig) -> AppConfig: app_config.dependencies["name"] = Provide(get_name) app_config.route_handlers.append(my_route_handler) return app_config app = Litestar(plugins=[MyPlugin()])