pub struct Error<C = ()>(/* private fields */);Expand description
type erased error object. can be used for dynamic access to error’s debug/display info. it also support upcasting and downcasting.
§Examples:
use std::{convert::Infallible, error, fmt};
use xitca_web::{error::Error, http::WebResponse, service::Service, WebContext};
// concrete error type
#[derive(Debug)]
struct Foo;
// implement debug and display format.
impl fmt::Display for Foo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Foo")
}
}
// implement Error trait
impl error::Error for Foo {}
// implement Service trait for http response generating.
impl<'r, C> Service<WebContext<'r, C>> for Foo {
type Response = WebResponse;
type Error = Infallible;
async fn call(&self, _: WebContext<'r, C>) -> Result<Self::Response, Self::Error> {
Ok(WebResponse::default())
}
}
async fn handle_error<C>(ctx: WebContext<'_, C>) {
// construct error object.
let e = Error::<C>::from_service(Foo);
// format and display error
println!("{e:?}");
println!("{e}");
// generate http response.
let res = Service::call(&e, ctx).await.unwrap();
assert_eq!(res.status().as_u16(), 200);
// upcast and downcast to concrete error type again.
// *. trait upcast is a nightly feature.
// see https://github.com/rust-lang/rust/issues/65991 for detail
// let e = &*e as &dyn error::Error;
// assert!(e.downcast_ref::<Foo>().is_some());
}Implementations§
source§impl<C> Error<C>
impl<C> Error<C>
pub fn from_service<S>(s: S) -> Selfwhere
S: for<'r> Service<WebContext<'r, C>, Response = WebResponse, Error = Infallible> + Error + Send + Sync + 'static,
Trait Implementations§
source§impl<C> Deref for Error<C>
impl<C> Deref for Error<C>
§type Target = dyn for<'r> ErrorService<WebContext<'r, C>, Response = Response<ResponseBody>, Error = Infallible>
type Target = dyn for<'r> ErrorService<WebContext<'r, C>, Response = Response<ResponseBody>, Error = Infallible>
The resulting type after dereferencing.
source§impl<C> Error for Error<C>
impl<C> Error for Error<C>
source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
The lower-level source of this error, if any. Read more
source§fn provide<'a>(&'a self, request: &mut Request<'a>)
fn provide<'a>(&'a self, request: &mut Request<'a>)
🔬This is a nightly-only experimental API. (
error_generic_member_access)Provides type based access to context intended for error reports. Read more
1.0.0 · source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
source§impl<C> From<BodyOverFlow> for Error<C>
impl<C> From<BodyOverFlow> for Error<C>
source§fn from(e: BodyOverFlow) -> Self
fn from(e: BodyOverFlow) -> Self
Converts to this type from the input type.
source§impl<C> From<EncodingError> for Error<C>
impl<C> From<EncodingError> for Error<C>
source§fn from(e: EncodingError) -> Self
fn from(e: EncodingError) -> Self
Converts to this type from the input type.
source§impl<C> From<ErrorStatus> for Error<C>
impl<C> From<ErrorStatus> for Error<C>
source§fn from(e: ErrorStatus) -> Self
fn from(e: ErrorStatus) -> Self
Converts to this type from the input type.
source§impl<C> From<ExtensionNotFound> for Error<C>
impl<C> From<ExtensionNotFound> for Error<C>
source§fn from(e: ExtensionNotFound) -> Self
fn from(e: ExtensionNotFound) -> Self
Converts to this type from the input type.
source§impl<C> From<HeaderNotFound> for Error<C>
impl<C> From<HeaderNotFound> for Error<C>
source§fn from(e: HeaderNotFound) -> Self
fn from(e: HeaderNotFound) -> Self
Converts to this type from the input type.
source§impl<C> From<Infallible> for Error<C>
impl<C> From<Infallible> for Error<C>
source§fn from(e: Infallible) -> Self
fn from(e: Infallible) -> Self
Converts to this type from the input type.
source§impl<C> From<InvalidHeaderValue> for Error<C>
impl<C> From<InvalidHeaderValue> for Error<C>
source§fn from(e: InvalidHeaderValue) -> Self
fn from(e: InvalidHeaderValue) -> Self
Converts to this type from the input type.
source§impl<C> From<MatchError> for Error<C>
impl<C> From<MatchError> for Error<C>
source§fn from(e: MatchError) -> Self
fn from(e: MatchError) -> Self
Converts to this type from the input type.
source§impl<C> From<MethodNotAllowed> for Error<C>
impl<C> From<MethodNotAllowed> for Error<C>
source§fn from(e: MethodNotAllowed) -> Self
fn from(e: MethodNotAllowed) -> Self
Converts to this type from the input type.
source§impl<C> From<ParseError> for Error<C>
impl<C> From<ParseError> for Error<C>
source§fn from(e: ParseError) -> Self
fn from(e: ParseError) -> Self
Converts to this type from the input type.
source§impl<C, E> From<Pipeline<Box<dyn Any + Send>, E>> for Error<C>
impl<C, E> From<Pipeline<Box<dyn Any + Send>, E>> for Error<C>
source§fn from(e: CatchUnwindError<E>) -> Self
fn from(e: CatchUnwindError<E>) -> Self
Converts to this type from the input type.
source§impl<E, C> From<RouterError<E>> for Error<C>where
E: Into<Self>,
impl<E, C> From<RouterError<E>> for Error<C>where
E: Into<Self>,
source§fn from(e: RouterError<E>) -> Self
fn from(e: RouterError<E>) -> Self
Converts to this type from the input type.
source§impl<C> From<StatusCode> for Error<C>
impl<C> From<StatusCode> for Error<C>
source§fn from(e: StatusCode) -> Self
fn from(e: StatusCode) -> Self
Converts to this type from the input type.
source§impl<C> From<ThreadJoinError> for Error<C>
impl<C> From<ThreadJoinError> for Error<C>
source§fn from(e: ThreadJoinError) -> Self
fn from(e: ThreadJoinError) -> Self
Converts to this type from the input type.
source§impl<C> From<ToStrError> for Error<C>
impl<C> From<ToStrError> for Error<C>
source§fn from(e: ToStrError) -> Self
fn from(e: ToStrError) -> Self
Converts to this type from the input type.
source§impl<'r, C, B> Responder<WebContext<'r, C, B>> for Error<C>
impl<'r, C, B> Responder<WebContext<'r, C, B>> for Error<C>
source§impl<'r, C> Service<WebContext<'r, C>> for Error<C>
impl<'r, C> Service<WebContext<'r, C>> for Error<C>
§type Response = Response<ResponseBody>
type Response = Response<ResponseBody>
The Ok part of output future.
§type Error = Infallible
type Error = Infallible
The Err part of output future.
async fn call( &self, ctx: WebContext<'r, C> ) -> Result<Self::Response, Self::Error>
Auto Trait Implementations§
impl<C = ()> !RefUnwindSafe for Error<C>
impl<C> Send for Error<C>
impl<C> Sync for Error<C>
impl<C> Unpin for Error<C>
impl<C = ()> !UnwindSafe for Error<C>
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
Mutably borrows from an owned value. Read more
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<S, Arg> ServiceExt<Arg> for Swhere
S: Service<Arg>,
impl<S, Arg> ServiceExt<Arg> for Swhere
S: Service<Arg>,
source§fn enclosed<T>(self, build: T) -> Pipeline<Self, T, BuildEnclosed>
fn enclosed<T>(self, build: T) -> Pipeline<Self, T, BuildEnclosed>
Enclose Self with given
T as Service<<Self as Service<_>>::Response>>. In other word T
would take Self’s Service::Response type as it’s generic argument of Service<_> impl.source§fn enclosed_fn<T, Req>(self, func: T) -> Pipeline<Self, T, BuildEnclosedFn>
fn enclosed_fn<T, Req>(self, func: T) -> Pipeline<Self, T, BuildEnclosedFn>
Function version of Self::enclosed method.
source§fn map<F, Res, ResMap>(self, mapper: F) -> Pipeline<Self, F, BuildMap>
fn map<F, Res, ResMap>(self, mapper: F) -> Pipeline<Self, F, BuildMap>
Mutate
<<Self::Response as Service<Req>>::Future as Future>::Output type with given
closure.