Struct problem_details::ProblemDetails
source · 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 moresource§impl<Ext: Debug> Debug for ProblemDetails<Ext>
impl<Ext: Debug> Debug for ProblemDetails<Ext>
source§impl<Ext: Default> Default for ProblemDetails<Ext>
impl<Ext: Default> Default for ProblemDetails<Ext>
source§fn default() -> ProblemDetails<Ext>
fn default() -> ProblemDetails<Ext>
source§impl<'de, Ext> Deserialize<'de> for ProblemDetails<Ext>where
Ext: Deserialize<'de>,
impl<'de, Ext> Deserialize<'de> for ProblemDetails<Ext>where
Ext: Deserialize<'de>,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<Ext> Display for ProblemDetails<Ext>
impl<Ext> Display for ProblemDetails<Ext>
source§impl<Ext> Error for ProblemDetails<Ext>where
Ext: Debug,
impl<Ext> Error for ProblemDetails<Ext>where
Ext: Debug,
1.30.0 · source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · source§fn description(&self) -> &str
fn description(&self) -> &str
source§impl<Ext> From<JsonProblemDetails<Ext>> for ProblemDetails<Ext>
impl<Ext> From<JsonProblemDetails<Ext>> for ProblemDetails<Ext>
source§fn from(value: JsonProblemDetails<Ext>) -> Self
fn from(value: JsonProblemDetails<Ext>) -> Self
source§impl<Ext> From<ProblemDetails<Ext>> for JsonProblemDetails<Ext>
impl<Ext> From<ProblemDetails<Ext>> for JsonProblemDetails<Ext>
source§fn from(value: ProblemDetails<Ext>) -> Self
fn from(value: ProblemDetails<Ext>) -> Self
source§impl<Ext> From<ProblemDetails<Ext>> for XmlProblemDetails<Ext>
impl<Ext> From<ProblemDetails<Ext>> for XmlProblemDetails<Ext>
source§fn from(value: ProblemDetails<Ext>) -> Self
fn from(value: ProblemDetails<Ext>) -> Self
source§impl<Ext> From<XmlProblemDetails<Ext>> for ProblemDetails<Ext>
impl<Ext> From<XmlProblemDetails<Ext>> for ProblemDetails<Ext>
source§fn from(value: XmlProblemDetails<Ext>) -> Self
fn from(value: XmlProblemDetails<Ext>) -> Self
source§impl<Ext> IntoResponse for ProblemDetails<Ext>where
Ext: Serialize,
impl<Ext> IntoResponse for ProblemDetails<Ext>where
Ext: Serialize,
source§fn into_response(self) -> Response
fn into_response(self) -> Response
source§impl<Ext> IntoResponse for ProblemDetails<Ext>
impl<Ext> IntoResponse for ProblemDetails<Ext>
source§fn into_response(self) -> Response
fn into_response(self) -> Response
Response
.source§fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>
impl IntoResponse
to add a header. Read moresource§fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>
impl IntoResponse
to with a new content type. Read moresource§fn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
fn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
impl IntoResponse
to set a status code. Read moresource§impl<Ext: PartialEq> PartialEq for ProblemDetails<Ext>
impl<Ext: PartialEq> PartialEq for ProblemDetails<Ext>
source§fn eq(&self, other: &ProblemDetails<Ext>) -> bool
fn eq(&self, other: &ProblemDetails<Ext>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<Ext> ResponseError for ProblemDetails<Ext>
impl<Ext> ResponseError for ProblemDetails<Ext>
source§fn status(&self) -> StatusCode
fn status(&self) -> StatusCode
source§fn as_response(&self) -> Response
fn as_response(&self) -> Response
source§impl<Ext> Serialize for ProblemDetails<Ext>where
Ext: Serialize,
impl<Ext> Serialize for ProblemDetails<Ext>where
Ext: Serialize,
impl<Ext: Eq> Eq for ProblemDetails<Ext>
impl<Ext> StructuralEq for ProblemDetails<Ext>
impl<Ext> StructuralPartialEq for ProblemDetails<Ext>
Auto Trait Implementations§
impl<Ext> RefUnwindSafe for ProblemDetails<Ext>where
Ext: RefUnwindSafe,
impl<Ext> Send for ProblemDetails<Ext>where
Ext: Send,
impl<Ext> Sync for ProblemDetails<Ext>where
Ext: Sync,
impl<Ext> Unpin for ProblemDetails<Ext>where
Ext: Unpin,
impl<Ext> UnwindSafe for ProblemDetails<Ext>where
Ext: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
source§fn call(
self,
_req: Request<Body>,
_state: S
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S ) -> <T as Handler<IntoResponseHandler, S>>::Future
source§fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
tower::Layer
] to the handler. Read moresource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Service
] by providing the statesource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
source§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Service
] and no state.source§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
MakeService
and no state. Read more§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoResult<T> for Twhere
T: IntoResponse,
impl<T> IntoResult<T> for Twhere
T: IntoResponse,
source§fn into_result(self) -> Result<T, Error>
fn into_result(self) -> Result<T, Error>
poem::Result<T>
.