pub struct ProblemDetails<Ext = ()> {
pub type: Option<ProblemType>,
pub status: Option<StatusCode>,
pub title: Option<String>,
pub detail: Option<String>,
pub instance: Option<Uri>,
pub extensions: Ext,
}
Expand description
A RFC 9457 / RFC 7807 problem details object.
§Creating problem details
You can create a new problem details from a given
status code using ProblemDetails::from_status_code
.
This will set the status
field to the given status code,
the title
field to the canonical reason phrase of the status code,
and the type
field to none, which is equivalent to about:blank
.
use http::StatusCode;
use problem_details::ProblemDetails;
let details = ProblemDetails::from_status_code(StatusCode::NOT_FOUND);
assert_eq!(details.status, Some(StatusCode::NOT_FOUND));
assert_eq!(details.title, Some("Not Found".to_string()));
assert_eq!(details.r#type.unwrap_or_default(), problem_details::ProblemType::default());
You can then use the builder pattern to add additional fields.
use http::{StatusCode, Uri};
use problem_details::ProblemDetails;
let details = ProblemDetails::from_status_code(StatusCode::NOT_FOUND)
.with_type(Uri::from_static("example:type"))
.with_title("There is something wrong");
assert_eq!(details.status, Some(StatusCode::NOT_FOUND));
assert_eq!(details.title, Some("There is something wrong".to_string()));
assert_eq!(details.r#type.unwrap_or_default(), Uri::from_static("example:type").into());
You can also create a new problem details object using ProblemDetails::new
.
use http::Uri;
use problem_details::ProblemDetails;
let details = ProblemDetails::new()
.with_type(Uri::from_static("example:type"))
.with_title("There is something wrong");
assert_eq!(details.status, None);
assert_eq!(details.title, Some("There is something wrong".to_string()));
assert_eq!(details.r#type.unwrap_or_default(), Uri::from_static("example:type").into());
§Extensions
To add extensions, you need to define a struct that holds the extension
fields, and use this struct as the generic parameter for ProblemDetails<Ext>
.
Using with_extensions
, the type is adjusted
automatically for you.
Extension fields are flattened into the problem details object when serialized.
use problem_details::ProblemDetails;
struct MyExt {
foo: String,
bar: u32,
}
let details = ProblemDetails::new()
.with_extensions(MyExt {
foo: "Hello".to_string(),
bar: 42,
});
// details is of type ProblemDetails<MyExt>
let typecheck: ProblemDetails<MyExt> = details;
If you need dynamic extensions, you can use a HashMap
as extensions object.
use std::collections::HashMap;
use problem_details::ProblemDetails;
let mut extensions = HashMap::<String, serde_json::Value>::new();
extensions.insert("foo".to_string(), serde_json::json!("Hello"));
extensions.insert("bar".to_string(), serde_json::json!(42));
let details = ProblemDetails::new()
.with_extensions(extensions);
// details is of type ProblemDetails<HashMap<String, serde_json::Value>>
let typecheck: ProblemDetails<HashMap<String, serde_json::Value>> = details;
Fields§
§type: Option<ProblemType>
An optional uri describing the problem type.
See https://www.rfc-editor.org/rfc/rfc9457.html#name-type for more information.
status: Option<StatusCode>
An optional status code for this problem.
See https://www.rfc-editor.org/rfc/rfc9457.html#name-status for more information.
title: Option<String>
An optional human-readable title for this problem.
See https://www.rfc-editor.org/rfc/rfc9457.html#name-title for more information.
detail: Option<String>
An optional human-readable description of this problem.
See https://www.rfc-editor.org/rfc/rfc9457.html#name-detail for more information.
instance: Option<Uri>
An optional uri identifying the specific instance of this problem.
See https://www.rfc-editor.org/rfc/rfc9457.html#name-instance for more information.
extensions: Ext
An object containing extensions to this problem details object.
Note that the extensions will be flattened into the resulting problem details representation.
See https://www.rfc-editor.org/rfc/rfc9457.html#name-extension-members for more information.
Implementations§
Source§impl ProblemDetails<()>
impl ProblemDetails<()>
Sourcepub fn from_status_code(status: StatusCode) -> Self
pub fn from_status_code(status: StatusCode) -> Self
Creates a new problem details object from a given status code.
This will set the status
field to the given status code,
the title
field to the canonical reason phrase of the status code,
and the type
field to none, which is equivalent to about:blank
.
Source§impl<Ext> ProblemDetails<Ext>
impl<Ext> ProblemDetails<Ext>
Sourcepub fn with_type(self, type: impl Into<ProblemType>) -> Self
pub fn with_type(self, type: impl Into<ProblemType>) -> Self
Builder-style method that sets the type
field of this problem details object.
Sourcepub fn with_status(self, status: impl Into<StatusCode>) -> Self
pub fn with_status(self, status: impl Into<StatusCode>) -> Self
Builder-style method that sets the status
field of this problem details object.
Sourcepub fn with_title(self, title: impl Into<String>) -> Self
pub fn with_title(self, title: impl Into<String>) -> Self
Builder-style method that sets the title
field of this problem details object.
Sourcepub fn with_detail(self, detail: impl Into<String>) -> Self
pub fn with_detail(self, detail: impl Into<String>) -> Self
Builder-style method that sets the detail
field of this problem details object.
Sourcepub fn with_instance(self, instance: impl Into<Uri>) -> Self
pub fn with_instance(self, instance: impl Into<Uri>) -> Self
Builder-style method that sets the instance
field of this problem details object.
Sourcepub fn with_extensions<NewExt>(
self,
extensions: NewExt,
) -> ProblemDetails<NewExt>
pub fn with_extensions<NewExt>( self, extensions: NewExt, ) -> ProblemDetails<NewExt>
Builder style method that sets the extensions
field of this probelm details object.
Trait Implementations§
Source§impl<Ext: Clone> Clone for ProblemDetails<Ext>
impl<Ext: Clone> Clone for ProblemDetails<Ext>
Source§fn clone(&self) -> ProblemDetails<Ext>
fn clone(&self) -> ProblemDetails<Ext>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more