[−][src]Trait evitable::EvitableError
Trait implemented for all error types generated by #[evitable]
.
Allows for creating new errors from the ErrorContext, and
allows getting the error kind.
Associated Types
type Kind: EvitableErrorKind
Error kind type. Generated by #[evitable]
.
type Context: ErrorContext<Error = Self, Kind = Self::Kind>
Error context type. The type marked by #[evitable]
.
Required methods
fn new(
context: Self::Context,
source: Option<Box<dyn StdError + Send + Sync + 'static>>
) -> Self
context: Self::Context,
source: Option<Box<dyn StdError + Send + Sync + 'static>>
) -> Self
Create a new error instance, based on an error context and an optional source error.
Instead of using this directly, see from_error_context when
wanting to create error instances with source errors, and from_context
when not. Derived implementations of this trait also implements From<ErrorContext>
, so using
from or into is also an option.
Arguments
context
- Error contextsource
- Optional error source
Example
#[evitable(description = "Error")] pub struct Context; // Later let error = Error::new(Context, None);
fn kind(&self) -> Self::Kind
Get the error kind.
Example
#[evitable] pub enum Context { #[evitable(description("Io error ({})", 0))] Io(u8), #[evitable(description = "Fmt error")] Fmt, } // Later let error = Error::new(Context::Io(42), None); let t = match error.kind() { evitable_context::ErrorKind::Io => "Io", evitable_context::ErrorKind::Fmt => "Fmt", _ => "Other", }; assert_eq!(t, "Io");
fn context(&self) -> &Self::Context
Get the error context.
fn backtrace(&self) -> &Backtrace
Get backtrace.
Provided methods
fn from_context(context: Self::Context) -> Self
Create a new error instance from an error context.
Arguments
context
- Error context
Example
#[evitable(description = "Error")] pub struct Context; // Later let error = Error::from_context(Context);
fn from_error_context<S: StdError + Send + Sync + 'static>(
context: Self::Context,
error: S
) -> Self
context: Self::Context,
error: S
) -> Self
Create a new error instance from an error context and a source error.
Arguments
context
- Error contextsource
- Error source
Example
#[evitable(description = "Error")] pub struct Context; // Later let io_error = io::Error::from(io::ErrorKind::NotFound); let error = Error::from_error_context(Context, io_error);