Module trackable::error [] [src]

Functionalities for implementing trackable errors and operating on those.

You can easily define your own trackable error types as follows:

#[macro_use]
extern crate trackable;
use trackable::error::{TrackableError, IntoTrackableError, ErrorKind, ErrorKindExt};

type MyError = TrackableError<MyErrorKind>;

#[derive(Debug, PartialEq, Eq)]
enum MyErrorKind {
    Critical,
    NonCritical,
}
impl ErrorKind for MyErrorKind {
    fn is_tracking_needed(&self) -> bool {
        *self == MyErrorKind::Critical  // Only critical errors are tracked
    }
}
impl IntoTrackableError<std::io::Error> for MyErrorKind {
    fn into_trackable_error(e: std::io::Error) -> MyError {
        // Any I/O errors are considered critical
        MyErrorKind::Critical.cause(e)
    }
}

fn main() {
    // Tracks an error
    let error: MyError = MyErrorKind::Critical.cause("something wrong");
    let error = track!(error);
    let error = track!(error, "I passed here");
    assert_eq!(format!("\nError: {}", error), r#"
Error: Critical (cause; something wrong)
HISTORY:
  [0] at rust_out:<anon>:28
  [1] at rust_out:<anon>:29; I passed here
"#);

    // Tries to execute I/O operation
    let result = (|| -> Result<_, MyError> {
        let f = track_try!(std::fs::File::open("/path/to/non_existent_file"));
        Ok(f)
    })();
    let error = result.err().unwrap();
    let cause = error.concrete_cause::<std::io::Error>().unwrap();
    assert_eq!(cause.kind(), std::io::ErrorKind::NotFound);
}

Structs

Failed

Built-in ErrorKind implementation which represents opaque errors.

TrackableError

Trackable error.

Enums

Event

An event that occurred on an instance of TrackableError.

Traits

ErrorKind

This trait represents a error kind which TrackableError can have.

ErrorKindExt

An extention of ErrorKind trait.

IntoTrackableError

This trait allows to construct TrackableError<Self> via a conversion from From.

Type Definitions

BoxError

Boxed Error object.

BoxErrorKind

Boxed ErrorKind object.

Failure

TrackableError type specialized for Failed.

History

History type specialized for TrackableError.