Struct http_api_problem::HttpApiProblem
source · [−]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",
}
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
sourceimpl HttpApiProblem
impl HttpApiProblem
sourcepub fn new<T: Into<StatusCode>>(status: T) -> Self
pub fn new<T: Into<StatusCode>>(status: T) -> Self
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);
sourcepub fn try_new<T: TryInto<StatusCode>>(
status: T
) -> Result<Self, InvalidStatusCode> where
T::Error: Into<InvalidStatusCode>,
pub fn try_new<T: TryInto<StatusCode>>(
status: T
) -> Result<Self, InvalidStatusCode> where
T::Error: Into<InvalidStatusCode>,
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);
sourcepub fn with_title<T: Into<StatusCode>>(status: T) -> Self
pub fn with_title<T: Into<StatusCode>>(status: T) -> Self
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);
sourcepub fn try_with_title<T: TryInto<StatusCode>>(
status: T
) -> Result<Self, InvalidStatusCode> where
T::Error: Into<InvalidStatusCode>,
pub fn try_with_title<T: TryInto<StatusCode>>(
status: T
) -> Result<Self, InvalidStatusCode> where
T::Error: Into<InvalidStatusCode>,
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);
sourcepub fn with_title_and_type<T: Into<StatusCode>>(status: T) -> Self
pub fn with_title_and_type<T: Into<StatusCode>>(status: T) -> Self
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);
sourcepub fn try_with_title_and_type<T: TryInto<StatusCode>>(
status: T
) -> Result<Self, InvalidStatusCode> where
T::Error: Into<InvalidStatusCode>,
pub fn try_with_title_and_type<T: TryInto<StatusCode>>(
status: T
) -> Result<Self, InvalidStatusCode> where
T::Error: Into<InvalidStatusCode>,
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);
sourcepub fn empty() -> Self
pub fn empty() -> Self
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
.
sourcepub fn status<T: Into<StatusCode>>(self, status: T) -> Self
pub fn status<T: Into<StatusCode>>(self, status: T) -> Self
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);
sourcepub fn type_url<T: Into<String>>(self, type_url: T) -> Self
pub fn type_url<T: Into<String>>(self, type_url: T) -> Self
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);
sourcepub fn try_status<T: TryInto<StatusCode>>(
self,
status: T
) -> Result<Self, InvalidStatusCode> where
T::Error: Into<InvalidStatusCode>,
pub fn try_status<T: TryInto<StatusCode>>(
self,
status: T
) -> Result<Self, InvalidStatusCode> where
T::Error: Into<InvalidStatusCode>,
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);
sourcepub fn title<T: Into<String>>(self, title: T) -> Self
pub fn title<T: Into<String>>(self, title: T) -> Self
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);
sourcepub fn detail<T: Into<String>>(self, detail: T) -> HttpApiProblem
pub fn detail<T: Into<String>>(self, detail: T) -> HttpApiProblem
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);
sourcepub fn instance<T: Into<String>>(self, instance: T) -> HttpApiProblem
pub fn instance<T: Into<String>>(self, instance: T) -> HttpApiProblem
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);
sourcepub fn try_value<K, V>(self, key: K, value: &V) -> Result<Self, String> where
V: Serialize,
K: Into<String>,
pub fn try_value<K, V>(self, key: K, value: &V) -> Result<Self, String> where
V: Serialize,
K: Into<String>,
Add a value that must be serializable.
The key must not be one of the field names of this struct.
sourcepub fn value<K, V>(self, key: K, value: &V) -> Self where
V: Serialize,
K: Into<String>,
pub fn value<K, V>(self, key: K, value: &V) -> Self where
V: Serialize,
K: Into<String>,
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.
pub fn set_value<K, V>(&mut self, key: K, value: &V) where
V: Serialize,
K: Into<String>,
sourcepub fn get_value<K, V>(&self, key: &str) -> Option<V> where
V: DeserializeOwned,
pub fn get_value<K, V>(&self, key: &str) -> Option<V> where
V: DeserializeOwned,
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
pub fn try_set_value<K, V>(&mut self, key: K, value: &V) -> Result<(), String> where
V: Serialize,
K: Into<String>,
pub fn keys<K, V>(&self) -> impl Iterator<Item = &String> where
V: DeserializeOwned,
sourcepub fn json_value(&self, key: &str) -> Option<&Value>
pub fn json_value(&self, key: &str) -> Option<&Value>
Returns the serde_json::Value
for the given key if the key exists.
sourcepub fn json_bytes(&self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
pub fn json_bytes(&self) -> Vec<u8>ⓘNotable traits for Vec<u8, A>impl<A> Write for Vec<u8, A> where
A: Allocator,
A: Allocator,
Serialize to a JSON Vec<u8>
sourcepub fn json_string(&self) -> String
pub fn json_string(&self) -> String
Serialize to a JSON String
sourcepub fn to_hyper_response(&self) -> Response<Body>
pub fn to_hyper_response(&self) -> Response<Body>
Creates a hyper response.
If status is None
500 - Internal Server Error
is the
default.
Requires the hyper
feature
sourcepub fn to_actix_response(&self) -> HttpResponse
pub fn to_actix_response(&self) -> HttpResponse
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
sourcepub fn to_rocket_response(&self) -> Response<'static>
pub fn to_rocket_response(&self) -> Response<'static>
Creates a rocket
response.
If status is None
500 - Internal Server Error
is the
default.
Requires the rocket
feature
sourcepub fn to_salvo_response(&self) -> Response
pub fn to_salvo_response(&self) -> Response
Creates a salvo response.
If status is None
500 - Internal Server Error
is the
default.
Requires the salvo
feature
sourcepub fn to_tide_response(&self) -> Response
pub fn to_tide_response(&self) -> Response
Creates a tide response.
If status is None
500 - Internal Server Error
is the
default.
Requires the tide
feature
pub fn with_title_from_status<T: Into<StatusCode>>(status: T) -> Self
please use with_title
instead
pub fn with_title_and_type_from_status<T: Into<StatusCode>>(status: T) -> Self
please use with_title_and_type
instead
pub fn set_status<T: Into<StatusCode>>(self, status: T) -> Self
please use status
instead
pub fn set_title<T: Into<String>>(self, title: T) -> Self
please use title
instead
pub fn set_detail<T: Into<String>>(self, detail: T) -> Self
please use detail
instead
pub fn set_type_url<T: Into<String>>(self, type_url: T) -> Self
please use type_url
instead
pub fn set_instance<T: Into<String>>(self, instance: T) -> Self
please use instance
instead
Trait Implementations
sourceimpl Clone for HttpApiProblem
impl Clone for HttpApiProblem
sourcefn clone(&self) -> HttpApiProblem
fn clone(&self) -> HttpApiProblem
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for HttpApiProblem
impl Debug for HttpApiProblem
sourceimpl<'de> Deserialize<'de> for HttpApiProblem
impl<'de> Deserialize<'de> for HttpApiProblem
sourcefn 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>,
Deserialize this value from the given Serde deserializer. Read more
sourceimpl Display for HttpApiProblem
impl Display for HttpApiProblem
sourceimpl Error for HttpApiProblem
impl Error for HttpApiProblem
sourcefn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
The lower-level source of this error, if any. Read more
sourcefn backtrace(&self) -> Option<&Backtrace>
fn backtrace(&self) -> Option<&Backtrace>
backtrace
)Returns a stack backtrace, if available, of where this error occurred. Read more
1.0.0 · sourcefn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
sourceimpl From<ApiError> for HttpApiProblem
impl From<ApiError> for HttpApiProblem
sourceimpl From<HttpApiProblem> for Response<Body>
impl From<HttpApiProblem> for Response<Body>
sourcefn from(problem: HttpApiProblem) -> Response<Body>
fn from(problem: HttpApiProblem) -> Response<Body>
Converts to this type from the input type.
sourceimpl From<HttpApiProblem> for HttpResponse
impl From<HttpApiProblem> for HttpResponse
sourcefn from(problem: HttpApiProblem) -> HttpResponse
fn from(problem: HttpApiProblem) -> HttpResponse
Converts to this type from the input type.
sourceimpl From<HttpApiProblem> for Response<'static>
impl From<HttpApiProblem> for Response<'static>
sourcefn from(problem: HttpApiProblem) -> Response<'static>
fn from(problem: HttpApiProblem) -> Response<'static>
Converts to this type from the input type.
sourceimpl From<HttpApiProblem> for Response
impl From<HttpApiProblem> for Response
sourcefn from(problem: HttpApiProblem) -> Response
fn from(problem: HttpApiProblem) -> Response
Converts to this type from the input type.
sourceimpl From<HttpApiProblem> for Response
impl From<HttpApiProblem> for Response
sourcefn from(problem: HttpApiProblem) -> Response
fn from(problem: HttpApiProblem) -> Response
Converts to this type from the input type.
sourceimpl From<StatusCode> for HttpApiProblem
impl From<StatusCode> for HttpApiProblem
sourcefn from(status: StatusCode) -> HttpApiProblem
fn from(status: StatusCode) -> HttpApiProblem
Converts to this type from the input type.
sourceimpl<'r> Responder<'r, 'static> for HttpApiProblem
impl<'r> Responder<'r, 'static> for HttpApiProblem
sourcefn respond_to(self, _request: &Request<'_>) -> Result<'static>
fn respond_to(self, _request: &Request<'_>) -> Result<'static>
Returns Ok
if a Response
could be generated successfully. Otherwise,
returns an Err
with a failing Status
. Read more
sourceimpl Serialize for HttpApiProblem
impl Serialize for HttpApiProblem
impl Reject for HttpApiProblem
Auto Trait Implementations
impl RefUnwindSafe for HttpApiProblem
impl Send for HttpApiProblem
impl Sync for HttpApiProblem
impl Unpin for HttpApiProblem
impl UnwindSafe for HttpApiProblem
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A> where
A: Array<Item = T>,
Converts self
into a collection.
fn mapped<U, F, A>(self, f: F) -> SmallVec<A> where
F: FnMut(T) -> U,
A: Array<Item = U>,
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more