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, 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 is_tracking_needed(&self) -> bool { *self == MyErrorKind::Critical // Only critical errors are tracked } } 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:30 [1] at src/error.rs:31 -- 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); }
Structs
Failed |
Built-in |
Failure |
|
TrackableError |
Trackable error. |
Enums
Event |
An event that occurred on an instance of |
Traits
ErrorKind |
This trait represents a error kind which |
ErrorKindExt |
An extention of |
Type Definitions
BoxError |
Boxed |
BoxErrorKind |
Boxed |
History |
|