Crate problemdetails

source ·
Expand description

This crate provides an implementation of a Problem Details response for HTTP APIs, as defined in RFC-7807. This is a standard way for HTTP APIs to indicate that a problem occurred with the request, including some standard payload fields as required.

When used with a supported HTTP Server, this will automatically generate the correct JSON response and set the Content-Type header to the correct value of application/problem+json.

If used with an unsupported HTTP Server, the status code and body of the problem details can be extracted and sent manually. The body field is in the correct structure to format into JSON using something like serde already, so serializing it should be as simple as the HTTP Server allows for.

Examples

Create an empty problem.

problemdetails::new(StatusCode::BAD_REQUEST);

Create a populated problem.

problemdetails::new(StatusCode::FORBIDDEN)
  .with_type("https://example.com/probs/out-of-credit")
  .with_title("You do not have enough credit.")
  .with_detail("Your current balance is 30, but that costs 50.")
  .with_instance("/account/12345/msgs/abc")
  .with_value("balance", 30)
  .with_value("accounts", vec!["/account/12345", "/account/67890"]);

Features

HTTP Server support is behind feature flags for the appropriate HTTP Server. As such, you will need to enable the correct feature for the HTTP Server that you are using.

Currently supported features are:

  • axum - For the Axum HTTP Server.

Structs

Representation of a Problem error to return to the client.

Functions

Create a new Problem response to send to the client.

Type Definitions

Result type where the error is always a Problem.