File: problem_details.rst

package info (click to toggle)
litestar 2.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,500 kB
  • sloc: python: 70,169; makefile: 254; javascript: 105; sh: 60
file content (40 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
===============
Problem Details
===============

.. versionadded:: 2.9.0

Problem details are a standardized way of providing machine-readable details of errors in HTTP
responses as specified in `RFC 9457`_, the latest RFC at the time of writing.

.. _RFC 9457: https://datatracker.ietf.org/doc/html/rfc9457

Usage
-----

To send a problem details response, the ``ProblemDetailsPlugin`` should be registered and then
a ``ProblemDetailsException`` can be raised anywhere which will automatically be converted
into a problem details response.

.. literalinclude:: /examples/plugins/problem_details/basic_usage.py
    :language: python
    :caption: Basic usage of the problem details plugin.

You can convert all ``HTTPExceptions`` into problem details response by enabling the flag in the ``ProblemDetailsConfig.``

.. literalinclude:: /examples/plugins/problem_details/convert_http_exceptions.py
    :language: python
    :caption: Converting ``HTTPException`` into problem details response.


You can also convert any exception that is not a ``HTTPException`` into a problem details response
by providing a mapping of the exception type to a callable that converts the exception into a
``ProblemDetailsException.``

.. tip:: This can be used to override how the ``HTTPException`` is converted into a problem details response as well.

.. literalinclude:: /examples/plugins/problem_details/convert_exceptions.py
    :language: python
    :caption: Converting custom exceptions into problem details response.

.. warning:: If the ``extra`` field is a ``Mapping``, then it's merged into the problem details response, otherwise it's included in the response with the key ``extra``.