Skip to main content

Module problem_details

Module problem_details 

Source
Expand description

RFC 7807 “Problem Details for HTTP APIs” — standardized error responses with application/problem+json. Sister to api_errors. See problem_details::ProblemDetails. RFC 7807 “Problem Details for HTTP APIs” — standardized error responses with the canonical application/problem+json content type.

Sister module to crate::api_errors: ApiError ships rustango’s flat {error, message, status, details} shape that frontends already parse. ProblemDetails ships the RFC 7807 shape that public REST APIs (Stripe, GitHub, Twitter all loose variants of it) and API gateways expect.

§Quick start

use rustango::problem_details::ProblemDetails;

async fn fetch_post(id: i64) -> Result<Json<Post>, ProblemDetails> {
    load(id).await.ok_or_else(|| ProblemDetails::not_found(
        format!("no post with id={id}"),
    ))
}
HTTP/1.1 404 Not Found
Content-Type: application/problem+json

{
  "type":   "about:blank",
  "title":  "Not Found",
  "status": 404,
  "detail": "no post with id=42"
}

§Extension fields

RFC 7807 explicitly allows arbitrary extra fields alongside the standard ones. Add them with [ProblemDetails::with_extension]:

ProblemDetails::validation("title cannot be empty")
    .with_extension("field", "title")
    .with_extension("rule", "non-empty")

§Interop with ApiError

ProblemDetails: From<ApiError> is implemented when both modules are on, so handlers that currently return Result<T, ApiError> can emit RFC 7807 by mapping at the boundary:

handler().await.map_err(ProblemDetails::from)

Structs§

ProblemDetails
One RFC 7807 problem document. Implements axum::response::IntoResponse so handlers can return Result<T, ProblemDetails> directly.