Skip to main content

problem_response

Function problem_response 

Source
pub fn problem_response(
    status: StatusCode,
    details: &ProblemDetails,
    content_language: Option<&str>,
) -> Response<String>
Expand description

An HTTP error response with an RFC 9457 problem details body, for any JMAP HTTP resource (draft-atwood-jmap-updates-00 §7.2: the Session resource, API, upload, download, event source, WebSocket opening handshake and extension resources SHOULD all return one on a 4xx or 5xx).

The Content-Type is application/problem+json. When the caller localized title or detail from the request’s Accept-Language (see JmapAccounts::accept_language), pass the language as content_language: it labels the response with Content-Language (RFC 8620 §3.8).

use http::StatusCode;
use jmap_types::problem::ProblemDetails;

let resp = jmap_server::problem_response(
    StatusCode::UNAUTHORIZED,
    &ProblemDetails::for_status(401).with_title("Nicht autorisiert"),
    Some("de"),
);
assert_eq!(resp.headers()["content-type"], "application/problem+json");
assert_eq!(resp.headers()["content-language"], "de");

§Panics

Does not panic in practice. Serializing details cannot fail (it holds only strings, an integer and a string-keyed map), and the response is built from a typed StatusCode, a constant Content-Type and a Content-Language that is added only when it parses as a header value.