pub struct Error { /* private fields */ }Expand description
General purpose Actix Web error.
An Actix Web error is used to carry errors from std::error through Actix in a convenient way.
It can be created through converting errors with into().
Whenever it is created from an external object a response error is created for it that can be
used to create an HTTP response from it this means that if you have access to an actix Error
you can always get a ResponseError reference from it.
Implementations§
Source§impl Error
impl Error
Sourcepub fn as_response_error(&self) -> &dyn ResponseError
pub fn as_response_error(&self) -> &dyn ResponseError
Returns the reference to the underlying ResponseError.
Sourcepub fn as_error<T: ResponseError + 'static>(&self) -> Option<&T>
pub fn as_error<T: ResponseError + 'static>(&self) -> Option<&T>
Similar to as_response_error but downcasts.
Sourcepub fn error_response(&self) -> HttpResponse
pub fn error_response(&self) -> HttpResponse
Shortcut for creating an HttpResponse.
Sourcepub fn add_response_mapper<F>(&mut self, mapper: F)
pub fn add_response_mapper<F>(&mut self, mapper: F)
Adds a function that maps the HTTP response generated for this error.
Mappers are called in the order they are added each time
error_response is called. A mapper may receive a response already
modified by other mappers, so it should avoid relying on a particular position in the chain.
Prefer narrowly mutating the provided response. Preserve fields the mapper does not own, insert default headers only when absent, and merge list-valued headers without introducing duplicates. Mappers should also be deterministic and safe to call more than once.
§Good
This mapper preserves the error response and adds a default only when it is absent:
use actix_web::{
error,
http::header::{self, HeaderValue},
};
let mut err = error::ErrorBadRequest("bad request");
err.add_response_mapper(|mut res| {
if !res.headers().contains_key(header::CACHE_CONTROL) {
res.headers_mut()
.insert(header::CACHE_CONTROL, HeaderValue::from_static("no-store"));
}
res
});
assert_eq!(
err.error_response().headers().get(header::CACHE_CONTROL),
Some(&HeaderValue::from_static("no-store")),
);§Bad
Replacing the response discards the original status, body, headers, extensions, and any changes made by earlier mappers:
use actix_web::{error, HttpResponse};
let mut err = error::ErrorBadRequest("bad request");
err.add_response_mapper(|_| HttpResponse::InternalServerError().finish());Trait Implementations§
Source§impl Error for Error
impl Error for Error
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
use the Display impl or to_string()