problem details#

Plugin for converting exceptions into a problem details response.

exception litestar.plugins.problem_details.ProblemDetailsException#

Bases: HTTPException

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.

Parameters:
  • *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.

  • type_ – The type field in the problem details.

  • title – The title field in the problem details.

  • instance – The instance field in the problem details.

to_response(request: Request[Any, Any, Any]) Response[dict[str, Any]]#

Convert the problem details exception into a Response.

class litestar.plugins.problem_details.ProblemDetailsConfig#

Bases: object

The 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 HTTPException into ProblemDetailsException.

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 ProblemDetailException before handling them. This can be used to override the handler for HTTPException as well.

__init__(exception_handler: ProblemDetailsExceptionHandlerType = <function _problem_details_exception_handler>, enable_for_all_http_exceptions: bool = False, exception_to_problem_detail_map: ExceptionToProblemDetailMapType = <factory>) None#
class litestar.plugins.problem_details.ProblemDetailsPlugin#

Bases: InitPluginProtocol

A 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 AppConfig instance after on_app_init hooks have been called.

Examples

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()])
Parameters:

app_config – The AppConfig instance.

Returns:

The app config object.