pub struct HttpWhatever { /* private fields */ }Expand description
An almost-drop-in replacement for snafu::Whatever with the following benefits:
- Conforms to the async magic incantation
Send + Sync + 'staticand so is thread-safe and async-safe - Can be transformed into an
http::Responseusing information from the error to complete the response - A public
newconstructor that facilitates better ergonomics in certain error situations. - A public
partsmethod to retrieve the three parts of the error.
Otherwise it is exactly the same as snafu::Whatever and can be used in exactly the same
way.
(almost-drop-in because, obviously, you have to use HttpWhatever as your error type).
Implementations§
Source§impl HttpWhatever
impl HttpWhatever
Sourcepub fn parts(&self) -> (&str, &str, StatusCode)
pub fn parts(&self) -> (&str, &str, StatusCode)
Return the three parts of the message as a 3-element tuple.
The three parts are
- The
messageas a string slice - the
domainas a string slice - the HTTP status code as a
http::StatusCode
This method is useful if you wish to construct a customized response from the error, but still want the categorization that this error type allows.
Sourcepub fn details(&self) -> String
pub fn details(&self) -> String
Return a String that provides the to_string() output of this error and all nested sources.
Sourcepub fn as_http_response<B>(&self) -> Response<B>where
B: Default,
pub fn as_http_response<B>(&self) -> Response<B>where
B: Default,
Return an http::Response<B> representation of the error, with
a body generated from the default method of the generic body type.
§Panics
Technically the function will panic if, internally, it cannot build
the response, but since the parts of the response are already verified,
and the Body is only created with B::default(), that isn’t actually
possible.
Sourcepub fn as_http_string_response<B>(&self) -> Response<B>
pub fn as_http_string_response<B>(&self) -> Response<B>
Return an http::Response<B> representation of the error, with
a string body generated from the into method of the generic body
type.
The string in the response body will be of the format
<message> (application domain: <domain>)
The content-type header of the response will be text/plain.
§Panics
Technically the function will panic if, internally, it cannot build
the response, but since the parts of the response are already verified,
and the Body is only created with B::from(String), that isn’t actually
possible.
Sourcepub fn as_http_json_response<B>(&self) -> Response<B>
pub fn as_http_json_response<B>(&self) -> Response<B>
Return an http::Response<B> representation of the error, with
a JSON body generated from the into method.
The string in the response body will be of the format
{"message":"<message>","domain":"<domain>"}
The content-type header of the response will be application/json.
§Panics
Technically the function will panic if, internally, it cannot build
the response, but since the parts of the response are already verified,
and the Body is only created with B::from(String), that isn’t actually
possible.
Trait Implementations§
Source§impl Debug for HttpWhatever
impl Debug for HttpWhatever
Source§impl Display for HttpWhatever
impl Display for HttpWhatever
Source§impl Error for HttpWhatever
impl Error for HttpWhatever
Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§fn cause(&self) -> Option<&dyn Error>
fn cause(&self) -> Option<&dyn Error>
Source§impl ErrorCompat for HttpWhatever
impl ErrorCompat for HttpWhatever
Source§fn iter_chain(&self) -> ChainCompat<'_, '_>where
Self: AsErrorSource,
fn iter_chain(&self) -> ChainCompat<'_, '_>where
Self: AsErrorSource,
Error::source. Read more