GitHub

problem_details

API reference for problem_details.

RFC 9457 problem details (Effect's http/ProblemDetails): the application/problem+json body a server sends to describe an error.

Schema is the lenient codec RFC 9457 asks for: a missing type is about:blank, a standard member of the wrong JSON type is dropped, and any other member is an extension, kept as it is. Encoding always writes type. to_response renders a problem. pyfect.http_api.error.Problem builds typed problem classes on the same members.

Values

AttributeTypeDescription
CONTENT_TYPELiteral['application/problem+json'] = 'application/problem+json'

The media type of a problem details body.

BLANK_TYPELiteral['about:blank'] = 'about:blank'

The problem type of a problem that only carries an HTTP status.

SchemaS.Bottom[ProblemDetails, dict[str, object]]

The lenient problem details codec (see the module docstring).

Classes

Functions

optional_member

optional_member[A, E](schema) -> S.Bottom[A, E, Any, Any]

A standard member left out when it has the wrong JSON type (RFC 9457 section 3.1); its key must be optional (NotRequired, or a default).

ParameterTypeDescription
schemaS.Codec[A, E, Any, Any]

Returns S.Bottom[A, E, Any, Any]

def optional_member[A, E](schema: S.Codec[A, E, Any, Any], /) -> S.Bottom[A, E, Any, Any]:
    """A standard member left out when it has the wrong JSON type (RFC 9457
    section 3.1); its key must be optional (``NotRequired``, or a default)."""

    def missing(_: object) -> Effect[Option[A], Any, Never]:
        return succeed(None)

    return schema.__codec__().pipe(S.catch_decoding(missing))

fixed_member

fixed_member[L: str | int](value) -> S.Bottom[L, L, Any, Any]

A member that always decodes to value, whatever (or whether) the body carries it; value is also its constructor default.

ParameterTypeDescription
valueL

Returns S.Bottom[L, L, Any, Any]

def fixed_member[L: str | int](value: L, /) -> S.Bottom[L, L, Any, Any]:
    """A member that always decodes to ``value``, whatever (or whether) the
    body carries it; ``value`` is also its constructor default."""
    literal = cast("LiteralString | int", value)
    defaulted = S.with_decoding_default_key(succeed(literal))(S.tag(literal))

    def fixed(_: object) -> Effect[Option[LiteralString | int], Any, Never]:
        return succeed(Some(literal))

    return cast("S.Bottom[L, L, Any, Any]", defaulted.pipe(S.catch_decoding(fixed)))

to_response

to_response(problem) -> HttpServerResponse

problem as an application/problem+json response, with its status (500 when it has none).

ParameterTypeDescription
problemProblemDetails

Returns HttpServerResponse

def to_response(problem: ProblemDetails) -> HttpServerResponse:
    """``problem`` as an ``application/problem+json`` response, with its
    ``status`` (500 when it has none)."""
    status = problem.get("status")
    return json_unsafe(
        problem, status=500 if status is None else int(status), content_type=CONTENT_TYPE
    )