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, 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);
}

Methods

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

Makes a new TrackableError instance.

Makes a new TrackableError instance from cause.

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<K: Debug> Debug for TrackableError<K>
[src]

Formats the value using the given formatter.

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.

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

A short description of the error. 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.

Assigns a randomly generated tracking number to this instance. Read more

Returns the tracking number of this instance if it has been assigned.

Enables tracking of this instance.

Disables tracking of this intance.

Returns the reference of the tracking history of this instance.

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

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

Returns true if tracking of this instance is enabled, otherwise false.