Struct trackable::error::TrackableError[][src]

pub struct TrackableError<K> { /* fields omitted */ }

Trackable error.

Examples

Defines your own Error type.

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

#[derive(Debug)]
struct MyError(TrackableError<MyErrorKind>);
derive_traits_for_trackable_error_newtype!(MyError, MyErrorKind);
impl From<std::io::Error> for MyError {
    fn from(f: std::io::Error) -> Self {
        // Any I/O errors are considered critical
        MyErrorKind::Critical.cause(f).into()
    }
}

#[derive(Debug, PartialEq, Eq)]
enum MyErrorKind {
    Critical,
    NonCritical,
}
impl ErrorKind for MyErrorKind {}

fn main() {
    // Tracks an error
    let error: MyError = MyErrorKind::Critical.cause("something wrong").into();
    let error = track!(error);
    let error = track!(error, "I passed here");
    assert_eq!(format!("\nError: {}", error).replace('\\', "/"), r#"
Error: Critical (cause; something wrong)
HISTORY:
  [0] at src/error.rs:27
  [1] at src/error.rs:28 -- I passed here
"#);

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

TrackableError is cloneable if K is so.

#[macro_use]
extern crate trackable;

use trackable::Trackable;
use trackable::error::{Failed, ErrorKindExt};

fn main() {
    let mut original = Failed.error();

    let original = track!(original, "Hello `original`!");
    let forked = original.clone();
    let forked = track!(forked, "Hello `forked`!");

    assert_eq!(format!("\n{}", original).replace('\\', "/"), r#"
Failed
HISTORY:
  [0] at src/error.rs:11 -- Hello `original`!
"#);

    assert_eq!(format!("\n{}", forked).replace('\\', "/"), r#"
Failed
HISTORY:
  [0] at src/error.rs:11 -- Hello `original`!
  [1] at src/error.rs:13 -- Hello `forked`!
"#);
}

Methods

impl<K: ErrorKind> TrackableError<K>
[src]

Makes a new TrackableError instance.

Returns the kind of this error.

Tries to return the cause of this error as a value of T type.

If neither this error has a cause nor it is an T value, this method will return None.

Trait Implementations

impl From<TrackableError<Failed>> for Failure
[src]

Performs the conversion.

impl From<Failure> for TrackableError<Failed>
[src]

Performs the conversion.

impl From<TrackableError<ErrorKind>> for IoError
[src]

Performs the conversion.

impl From<IoError> for TrackableError<ErrorKind>
[src]

Performs the conversion.

impl<K: Debug> Debug for TrackableError<K>
[src]

Formats the value using the given formatter. Read more

impl<K: Clone> Clone for TrackableError<K>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<K: ErrorKind> From<K> for TrackableError<K>
[src]

Performs the conversion.

impl<K: ErrorKind + Default> Default for TrackableError<K>
[src]

Returns the "default value" for a type. Read more

impl<K: ErrorKind> Display for TrackableError<K>
[src]

Formats the value using the given formatter. Read more

impl<K: ErrorKind> Error for TrackableError<K>
[src]

This method is soft-deprecated. Read more

The lower-level cause of this error, if any. Read more

impl<K> Trackable for TrackableError<K>
[src]

Event type which a history of an instance of this type can have.

Returns the reference of the tracking history of this instance. Read more

Returns the mutable reference of the tracking history of this instance. Read more

Add an event into the tail of the history of this instance. Read more

Returns true if it is being tracked, otherwise false.

Auto Trait Implementations

impl<K> Send for TrackableError<K> where
    K: Send

impl<K> Sync for TrackableError<K> where
    K: Sync