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§
Source§impl 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>
pub fn try_builder<S: TryInto<StatusCode>>( status: S, ) -> Result<ApiErrorBuilder, 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>
pub fn try_new<S: TryInto<StatusCode>>( status: S, ) -> Result<Self, 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 fields_mut(&mut self) -> &mut HashMap<String, Value>
pub fn fields_mut(&mut self) -> &mut HashMap<String, Value>
Returns a mutable reference to the serialized fields
Sourcepub fn extensions(&self) -> &Extensions
pub fn extensions(&self) -> &Extensions
Get a reference to the extensions
Extensions will not be part of an HttpApiProblem
Sourcepub fn extensions_mut(&mut self) -> &mut Extensions
pub fn extensions_mut(&mut self) -> &mut Extensions
Get a mutable reference to the extensions
Extensions will not be part of an HttpApiProblem
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<String>
pub fn into_hyper_response(self) -> Response<String>
Creates a hyper response containing a problem JSON.
Requires the hyper
feature
Sourcepub fn into_axum_response(self) -> Response
pub fn into_axum_response(self) -> Response
Creates an axum Response containing a problem JSON.
Requires the axum
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§
Source§impl Error for ApiError
impl Error for ApiError
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 From<ApiError> for HttpApiProblem
impl From<ApiError> for HttpApiProblem
Source§impl From<ApiError> for HttpResponse
impl From<ApiError> for HttpResponse
Source§impl From<ApiErrorBuilder> for ApiError
impl From<ApiErrorBuilder> for ApiError
Source§fn from(builder: ApiErrorBuilder) -> Self
fn from(builder: ApiErrorBuilder) -> Self
Source§impl From<Infallible> for ApiError
impl From<Infallible> for ApiError
Source§fn from(error: Infallible) -> Self
fn from(error: Infallible) -> Self
Source§impl From<MailboxError> for ApiError
impl From<MailboxError> for ApiError
Source§fn from(error: MailboxError) -> Self
fn from(error: MailboxError) -> Self
Source§impl From<StatusCode> for ApiError
impl From<StatusCode> for ApiError
Source§fn from(s: StatusCode) -> Self
fn from(s: StatusCode) -> Self
Source§impl<T: IntoApiError> From<T> for ApiError
impl<T: IntoApiError> From<T> for ApiError
Source§impl IntoResponse for ApiError
impl IntoResponse for ApiError
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Source§impl ResponseError for ApiError
impl ResponseError for ApiError
Source§fn error_response(&self) -> HttpResponse
fn error_response(&self) -> HttpResponse
Source§fn status_code(&self) -> StatusCode
fn status_code(&self) -> StatusCode
impl Reject for ApiError
Auto Trait Implementations§
impl Freeze for ApiError
impl !RefUnwindSafe for ApiError
impl Send for ApiError
impl Sync for ApiError
impl Unpin for ApiError
impl !UnwindSafe for ApiError
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);