Struct http_api_problem::ApiError
source · [−]pub struct ApiError { /* private fields */ }
Expand description
An error that should be returned from an API handler of a web service.
This should be returned from a handler as an error instead of a response or HttpApiProblem. Allows for logging etc. right before a response is generated.
Advantage over using an HttpApiProblem directly are that the StatusCode is mandatory and that this struct can also capture a source error which the HttpApiProblem does not since no error chains should be transmitted to clients.
Message on Display and converting to HttpApiProblem
When Display::fmt is invoked or when the details field of an HttpApiProblem
is filled, the message
field is used if present. If no message
is set
but there is a source
error set, to_string()
of the source will
be used instead. Otherwise nothing will be displayed or set.
ApiError
requires the feature api-error
to be enabled.
Implementations
sourceimpl ApiError
impl ApiError
sourcepub fn builder<T: Into<StatusCode>>(status: T) -> ApiErrorBuilder
pub fn builder<T: Into<StatusCode>>(status: T) -> ApiErrorBuilder
Get an ApiErrorBuilder with the given StatusCode preset.
sourcepub fn try_builder<S: TryInto<StatusCode>>(
status: S
) -> Result<ApiErrorBuilder, InvalidStatusCode> where
S::Error: Into<InvalidStatusCode>,
pub fn try_builder<S: TryInto<StatusCode>>(
status: S
) -> Result<ApiErrorBuilder, InvalidStatusCode> where
S::Error: Into<InvalidStatusCode>,
Try to get an ApiErrorBuilder with the given StatusCode preset.
Fails if the status
argument can not be converted to a StatusCode
sourcepub fn new<T: Into<StatusCode>>(status: T) -> Self
pub fn new<T: Into<StatusCode>>(status: T) -> Self
Create a new instance with the given StatusCode
sourcepub fn try_new<S: TryInto<StatusCode>>(
status: S
) -> Result<Self, InvalidStatusCode> where
S::Error: Into<InvalidStatusCode>,
pub fn try_new<S: TryInto<StatusCode>>(
status: S
) -> Result<Self, InvalidStatusCode> where
S::Error: Into<InvalidStatusCode>,
Try to create a new instance with the given StatusCode
Fails if the status
argument can not be converted to a StatusCode
sourcepub fn set_status<T: Into<StatusCode>>(&mut self, status: T)
pub fn set_status<T: Into<StatusCode>>(&mut self, status: T)
Set the StatusCode.
sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
Get the StatusCode.
sourcepub fn set_title<T: Display>(&mut self, title: T)
pub fn set_title<T: Display>(&mut self, title: T)
This is an optional title which can be used to create a valuable output for consumers.
sourcepub fn title(&self) -> Option<&str>
pub fn title(&self) -> Option<&str>
This is an optional title which can be used to create a valuable output for consumers.
sourcepub fn set_message<T: Display>(&mut self, message: T)
pub fn set_message<T: Display>(&mut self, message: T)
Set a message that describes the error in a human readable form.
sourcepub fn message(&self) -> Option<&str>
pub fn message(&self) -> Option<&str>
A message that describes the error in a human readable form.
sourcepub fn set_type_url<T: Display>(&mut self, type_url: T)
pub fn set_type_url<T: Display>(&mut self, type_url: T)
Set a URL that points to a detailed description of the error.
If not set it will most probably become httpstatus.es.com/XXX
when
the problem response is generated.
sourcepub fn type_url(&self) -> Option<&str>
pub fn type_url(&self) -> Option<&str>
A URL that points to a detailed description of the error.
pub fn set_instance<T: Display>(&mut self, instance: T)
sourcepub fn instance(&self) -> Option<&str>
pub fn instance(&self) -> Option<&str>
A URL that points to a detailed description of the error.
pub fn set_source<E: Error + Send + Sync + 'static>(&mut self, source: E)
pub fn set_source_in_a_box<E: Into<Box<dyn Error + Send + Sync + 'static>>>(
&mut self,
source: E
)
sourcepub fn add_field<T: Into<String>, V: Serialize>(
&mut self,
name: T,
value: V
) -> bool
pub fn add_field<T: Into<String>, V: Serialize>(
&mut self,
name: T,
value: V
) -> bool
Adds a serializable field. If the serialization fails nothing will be
added. This method returns true
if the field was added and false
if
the field could not be added.
An already present field with the same name will be replaced.
sourcepub fn try_add_field<T: Into<String>, V: Serialize>(
&mut self,
name: T,
value: V
) -> Result<(), Box<dyn Error + 'static>>
pub fn try_add_field<T: Into<String>, V: Serialize>(
&mut self,
name: T,
value: V
) -> Result<(), Box<dyn Error + 'static>>
Adds a serializable field. If the serialization fails nothing will be added. This fails if a failure occurred while adding the field.
An already present field with the same name will be replaced.
sourcepub fn to_http_api_problem(&self) -> HttpApiProblem
pub fn to_http_api_problem(&self) -> HttpApiProblem
Creates an HttpApiProblem from this.
Note: If the status is StatusCode::UNAUTHORIZED fields will not be put into the problem.
sourcepub fn into_http_api_problem(self) -> HttpApiProblem
pub fn into_http_api_problem(self) -> HttpApiProblem
Turns this into an HttpApiProblem.
Note: If the status is StatusCode::UNAUTHORIZED fields will not be put into the problem.
sourcepub fn detail_message(&self) -> Option<Cow<'_, str>>
pub fn detail_message(&self) -> Option<Cow<'_, str>>
If there is a message it will be the message otherwise the source error stringified
If none is present, None
is returned
sourcepub fn into_hyper_response(self) -> Response<Body>
pub fn into_hyper_response(self) -> Response<Body>
Creates a hyper response containing a problem JSON.
Requires the hyper
feature
sourcepub fn into_actix_web_response(self) -> HttpResponse
pub fn into_actix_web_response(self) -> HttpResponse
Creates a actix-web
response containing a problem JSON.
Requires the actix.web
feature
sourcepub fn into_salvo_response(self) -> Response
pub fn into_salvo_response(self) -> Response
Creates a salvo response containing a problem JSON.
Requires the salvo
feature
sourcepub fn into_tide_response(self) -> Response
pub fn into_tide_response(self) -> Response
Creates a tide response containing a problem JSON.
Requires the tide
feature
Trait Implementations
sourceimpl Error for ApiError
impl Error for ApiError
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<ApiError> for HttpResponse
impl From<ApiError> for HttpResponse
sourceimpl From<ApiErrorBuilder> for ApiError
impl From<ApiErrorBuilder> for ApiError
sourcefn from(builder: ApiErrorBuilder) -> Self
fn from(builder: ApiErrorBuilder) -> Self
Converts to this type from the input type.
sourceimpl From<MailboxError> for ApiError
impl From<MailboxError> for ApiError
sourcefn from(error: MailboxError) -> Self
fn from(error: MailboxError) -> Self
Converts to this type from the input type.
sourceimpl From<StatusCode> for ApiError
impl From<StatusCode> for ApiError
sourcefn from(s: StatusCode) -> Self
fn from(s: StatusCode) -> Self
Converts to this type from the input type.
sourceimpl<T: IntoApiError> From<T> for ApiError
impl<T: IntoApiError> From<T> for ApiError
sourceimpl ResponseError for ApiError
impl ResponseError for ApiError
sourcefn error_response(&self) -> HttpResponse
fn error_response(&self) -> HttpResponse
Creates full response for error. Read more
sourcefn status_code(&self) -> StatusCode
fn status_code(&self) -> StatusCode
Returns appropriate status code for error. Read more
impl Reject for ApiError
Auto Trait Implementations
impl !RefUnwindSafe for ApiError
impl Send for ApiError
impl Sync for ApiError
impl Unpin for ApiError
impl !UnwindSafe for ApiError
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>,
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