[][src]Trait evitable::EvitableError

pub trait EvitableError: StdError + Sized + 'static {
    type Kind: EvitableErrorKind;
    type Context: ErrorContext<Error = Self, Kind = Self::Kind>;
    fn new(
        context: Self::Context,
        source: Option<Box<dyn StdError + 'static>>
    ) -> Self;
fn kind(&self) -> Self::Kind;
fn context(&self) -> &Self::Context;
fn backtrace(&self) -> &Backtrace; fn from_context(context: Self::Context) -> Self { ... }
fn from_error_context<S: StdError + 'static>(
        context: Self::Context,
        error: S
    ) -> Self { ... } }

Trait implemented for all error types generated by #[derive(ErrorContext)]. Allows for creating new errors from the ErrorContext, and allows getting the error kind.

Associated Types

type Kind: EvitableErrorKind

Error kind type. Generated by #[derive(ErrorContext)].

type Context: ErrorContext<Error = Self, Kind = Self::Kind>

Error context type. The type marked by #[derive(ErrorContext)].

Loading content...

Required methods

fn new(
    context: Self::Context,
    source: Option<Box<dyn StdError + '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 context
  • source - Optional error source

Example

 #[derive(ErrorContext)]
 #[evitable(description = "Error")]
 pub struct Context;

 // Later
 let error = Error::new(Context, None);

fn kind(&self) -> Self::Kind

Get the error kind.

Example

 #[derive(ErrorContext)]
 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.

Loading content...

Provided methods

fn from_context(context: Self::Context) -> Self

Create a new error instance from an error context.

Arguments

  • context - Error context

Example

 #[derive(ErrorContext)]
 #[evitable(description = "Error")]
 pub struct Context;

 // Later
 let error = Error::from_context(Context);

fn from_error_context<S: StdError + 'static>(
    context: Self::Context,
    error: S
) -> Self

Create a new error instance from an error context and a source error.

Arguments

  • context - Error context
  • source - Error source

Example

 #[derive(ErrorContext)]
 #[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);
Loading content...

Implementors

Loading content...