rama::error

Trait ErrorExt

pub trait ErrorExt: SealedErrorExt {
    // Required methods
    fn context<M>(self, context: M) -> OpaqueError
       where M: Display + Send + Sync + 'static;
    fn with_context<C, F>(self, context: F) -> OpaqueError
       where C: Display + Send + Sync + 'static,
             F: FnOnce() -> C;
    fn backtrace(self) -> OpaqueError;
    fn into_opaque(self) -> OpaqueError;
}
Expand description

Extends the Error type with methods for working with errorss.

See the module level documentation for more information.

§Examples

use rama_error::{BoxError, ErrorExt, ErrorContext};

#[derive(Debug)]
struct CustomError;

impl std::fmt::Display for CustomError {
 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
   write!(f, "Custom error")
 }
}

impl std::error::Error for CustomError {}

let error = CustomError.context("whoops");
assert_eq!(error.to_string(), "whoops\r\n ↪ Custom error");

Required Methods§

fn context<M>(self, context: M) -> OpaqueError
where M: Display + Send + Sync + 'static,

Wrap the error in a context.

§Examples
use rama_error::ErrorExt;

let error = std::io::Error::new(std::io::ErrorKind::Other, "oh no!").context("do I/O");
assert_eq!(error.to_string(), "do I/O\r\n ↪ oh no!");

fn with_context<C, F>(self, context: F) -> OpaqueError
where C: Display + Send + Sync + 'static, F: FnOnce() -> C,

Lazily wrap the error with a context.

§Examples
use rama_error::ErrorExt;

let error = std::io::Error::new(std::io::ErrorKind::Other, "oh no!").with_context(|| format!(
   "do I/O ({})", 42,
));
assert_eq!(error.to_string(), "do I/O (42)\r\n ↪ oh no!");

fn backtrace(self) -> OpaqueError

Add a Backtrace to the error.

§Examples
use rama_error::ErrorExt;

let error = std::io::Error::new(std::io::ErrorKind::Other, "oh no!").backtrace();
println!("{}", error);

fn into_opaque(self) -> OpaqueError

Convert the error into an OpaqueError.

§Examples
use rama_error::ErrorExt;

let error = std::io::Error::new(std::io::ErrorKind::Other, "oh no!").into_opaque();
assert_eq!(error.to_string(), "oh no!");

Object Safety§

This trait is not object safe.

Implementors§

§

impl<Error> ErrorExt for Error
where Error: Error + Send + Sync + 'static,