pub struct HttpApiProblem {
    pub type_url: Option<String>,
    pub status: Option<StatusCode>,
    pub title: Option<String>,
    pub detail: Option<String>,
    pub instance: Option<String>,
    /* private fields */
}
Expand description

Description of a problem that can be returned by an HTTP API based on RFC7807

Example

{
   "type": "https://example.com/probs/out-of-credit",
   "title": "You do not have enough credit.",
   "detail": "Your current balance is 30, but that costs 50.",
   "instance": "/account/12345/msgs/abc",
}

Purpose

The purpose of HttpApiProblem is to generate a meaningful response for clients. It is not intended to be used as a replacement for a proper Error struct within applications.

For a struct which can be returned by HTTP handlers use ApiError which can be enabled with the feature toggle api-error. ApiError can be directly converted into HttpApiProblem.

Status Codes and Responses

Prefer to use one of the constructors which ensure that a StatusCode is set. If no StatusCode is set and a transformation to a response of a web framework is made a StatusCode becomes mandatory which in this case will default to 500.

When receiving an HttpApiProblem there might be an invalid StatusCode contained. In this case the status field will be empty. This is a trade off so that the recipient does not have to deal with another error and can still have access to the remaining fields of the struct.

Fields§

§type_url: Option<String>

A URI reference RFC3986 that identifies the problem type. This specification encourages that, when dereferenced, it provide human-readable documentation for the problem type (e.g., using HTML [W3C.REC-html5-20141028]). When this member is not present, its value is assumed to be “about:blank”.

§status: Option<StatusCode>

The HTTP status code RFC7231, Section 6 generated by the origin server for this occurrence of the problem.

§title: Option<String>

A short, human-readable summary of the problem type. It SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization (e.g., using proactive content negotiation; see RFC7231, Section 3.4.

§detail: Option<String>

A human-readable explanation specific to this occurrence of the problem.

§instance: Option<String>

A URI reference that identifies the specific occurrence of the problem. It may or may not yield further information if dereferenced.

Implementations§

Creates a new instance with the given StatusCode.

#Example

use http_api_problem::*;

let p = HttpApiProblem::new(StatusCode::INTERNAL_SERVER_ERROR);

assert_eq!(Some(StatusCode::INTERNAL_SERVER_ERROR), p.status);
assert_eq!(None, p.title);
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);

Creates a new instance with the given StatusCode.

Fails if the argument can not be converted into a StatusCode.

#Example

use http_api_problem::*;

let p = HttpApiProblem::try_new(500).unwrap();

assert_eq!(Some(StatusCode::INTERNAL_SERVER_ERROR), p.status);
assert_eq!(None, p.title);
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);

Creates a new instance with title derived from a StatusCode.

#Example

use http_api_problem::*;

let p = HttpApiProblem::with_title(StatusCode::NOT_FOUND);

assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Not Found"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);

Creates a new instance with title derived from a StatusCode.

Fails if the argument can not be converted into a StatusCode.

#Example

use http_api_problem::*;

let p = HttpApiProblem::try_with_title(404).unwrap();

assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Not Found"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);

Creates a new instance with the title and type_url derived from the StatusCode.

#Example

use http_api_problem::*;

let p = HttpApiProblem::with_title_and_type(StatusCode::SERVICE_UNAVAILABLE);

assert_eq!(Some(StatusCode::SERVICE_UNAVAILABLE), p.status);
assert_eq!(Some("Service Unavailable"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(Some("https://httpstatuses.com/503".to_string()), p.type_url);
assert_eq!(None, p.instance);

Creates a new instance with the title and type_url derived from the StatusCode.

Fails if the argument can not be converted into a StatusCode.

#Example

use http_api_problem::*;

let p = HttpApiProblem::try_with_title_and_type(503).unwrap();

assert_eq!(Some(StatusCode::SERVICE_UNAVAILABLE), p.status);
assert_eq!(Some("Service Unavailable"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(Some("https://httpstatuses.com/503".to_string()), p.type_url);
assert_eq!(None, p.instance);

Creates a new instance without any field set.

Prefer to use one of the other constructors which ensure that a StatusCode is set. If no StatusCode is set and a transformation to a response of a web framework is made a StatusCode becomes mandatory which in this case will default to 500.

Sets the status

#Example

use http_api_problem::*;

let p = HttpApiProblem::new(StatusCode::NOT_FOUND).title("Error");

assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Error"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);

Sets the type_url

#Example

use http_api_problem::*;

let p = HttpApiProblem::new(StatusCode::NOT_FOUND).type_url("http://example.com/my/real_error");

assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(None, p.title);
assert_eq!(None, p.detail);
assert_eq!(Some("http://example.com/my/real_error".to_string()), p.type_url);
assert_eq!(None, p.instance);

Tries to set the status

Fails if the argument can not be converted into a StatusCode.

#Example

use http_api_problem::*;

let p = HttpApiProblem::try_new(404).unwrap().title("Error");

assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Error"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);

Sets the title

#Example

use http_api_problem::*;

let p = HttpApiProblem::new(StatusCode::NOT_FOUND).title("Another Error");

assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(Some("Another Error"), p.title.as_deref());
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);

Sets the detail

#Example

use http_api_problem::*;

let p = HttpApiProblem::new(StatusCode::NOT_FOUND).detail("a detailed description");

assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(None, p.title);
assert_eq!(Some("a detailed description".to_string()), p.detail);
assert_eq!(None, p.type_url);
assert_eq!(None, p.instance);

Sets the instance

#Example

use http_api_problem::*;

let p = HttpApiProblem::new(StatusCode::NOT_FOUND).instance("/account/1234/withdraw");

assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!(None, p.title);
assert_eq!(None, p.detail);
assert_eq!(None, p.type_url);
assert_eq!(Some("/account/1234/withdraw".to_string()), p.instance);

Add a value that must be serializable.

The key must not be one of the field names of this struct.

These values get serialized into the JSON on top level.

Add a value that must be serializable.

The key must not be one of the field names of this struct. If the key is a field name or the value is not serializable nothing happens.

These values get serialized into the JSON on top level.

Add a value that must be serializable.

The key must not be one of the field names of this struct. If the key is a field name or the value is not serializable nothing happens.

These values get serialized into the JSON on top level.

Returns the deserialized field for the given key.

If the key does not exist or the field is not deserializable to the target type None is returned

Returns a reference to the serialized fields

If the key does not exist or the field is not deserializable to the target type None is returned

Returns a mutable reference to the serialized fields

If the key does not exist or the field is not deserializable to the target type None is returned

Returns the serde_json::Value for the given key if the key exists.

Serialize to a JSON Vec<u8>

Serialize to a JSON String

Creates a hyper response.

If status is None 500 - Internal Server Error is the default.

Requires the hyper feature

Creates an axum Response.

If status is None 500 - Internal Server Error is the default.

Requires the axum feature

Creates an actix response.

If status is None or not convertible to an actix status 500 - Internal Server Error is the default.

Requires the actix-web feature

Creates a rocket response.

If status is None 500 - Internal Server Error is the default.

Requires the rocket feature

Creates a salvo response.

If status is None 500 - Internal Server Error is the default.

Requires the salvo feature

Creates a tide response.

If status is None 500 - Internal Server Error is the default.

Requires the tide feature

👎Deprecated since 0.50.0: please use with_title instead
👎Deprecated since 0.50.0: please use with_title_and_type instead
👎Deprecated since 0.50.0: please use status instead
👎Deprecated since 0.50.0: please use title instead
👎Deprecated since 0.50.0: please use detail instead
👎Deprecated since 0.50.0: please use type_url instead
👎Deprecated since 0.50.0: please use instance instead

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
The lower-level source of this error, if any. Read more
👎Deprecated since 1.42.0: use the Display impl or to_string()
👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type based access to context intended for error reports. Read more
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Converts to this type from the input type.
Create a response.
The name of the generated JSON Schema. Read more
Generates a JSON Schema for this type. Read more
Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
Create the responses type, which is a list of responses that can be rendered in openapi.json format. Read more
Returns Ok if a Response could be generated successfully. Otherwise, returns an Err with a failing Status. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Converts to this type from a reference to the input type.
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts self into a collection.
Create the responses type, which is a list of responses that can be rendered in openapi.json format. Read more
🔬This is a nightly-only experimental API. (provide_any)
Data providers should implement this method to provide all values they are able to provide by using demand. Read more
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more