pub trait EvitableError:
StdError
+ Sized
+ Send
+ Sync
+ 'static {
type Kind: EvitableErrorKind;
type Context: ErrorContext<Error = Self, Kind = Self::Kind>;
// Required methods
fn new(
context: Self::Context,
source: Option<Box<dyn StdError + Send + Sync + 'static>>,
) -> Self;
fn kind(&self) -> Self::Kind;
fn context(&self) -> &Self::Context;
fn backtrace(&self) -> &Backtrace;
// Provided methods
fn from_context(context: Self::Context) -> Self { ... }
fn from_error_context<S: StdError + Send + Sync + 'static>(
context: Self::Context,
error: S,
) -> Self { ... }
}Expand description
Trait implemented for all error types generated by #[evitable].
Allows for creating new errors from the ErrorContext, and
allows getting the error kind.
Required Associated Types§
Sourcetype Kind: EvitableErrorKind
type Kind: EvitableErrorKind
Error kind type. Generated by #[evitable].
Sourcetype Context: ErrorContext<Error = Self, Kind = Self::Kind>
type Context: ErrorContext<Error = Self, Kind = Self::Kind>
Error context type. The type marked by #[evitable].
Required Methods§
Sourcefn new(
context: Self::Context,
source: Option<Box<dyn StdError + Send + Sync + 'static>>,
) -> Self
fn new( 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);Sourcefn kind(&self) -> Self::Kind
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");Provided Methods§
Sourcefn from_context(context: Self::Context) -> Self
fn from_context(context: Self::Context) -> Self
Sourcefn from_error_context<S: StdError + Send + Sync + 'static>(
context: Self::Context,
error: S,
) -> Self
fn from_error_context<S: StdError + Send + Sync + 'static>( 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);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.