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 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), r#" Error: Critical (cause; something wrong) HISTORY: [0] at <anon>:30 [1] at <anon>: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); }
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(); original.assign_tracking_number(); let original = track!(original, "Hello `original`!"); let forked = original.clone(); let forked = track!(forked, "Hello `forked`!"); assert_eq!(format!("\n{}", original), r#" Failed #4d6fdaeeb2cc39a2 HISTORY: [0] at <anon>:11 -- Hello `original`! "#); assert_eq!(format!("\n{}", forked), r#" Failed #4d6fdaeeb2cc39a2 HISTORY: [0] at <anon>:11 -- Hello `original`! [1] at <anon>:13 -- Hello `forked`! "#); }
Methods
impl<K: ErrorKind> TrackableError<K>
[src]
fn new<E>(kind: K, cause: E) -> Self where
E: Into<BoxError>,
E: Into<BoxError>,
Makes a new TrackableError
instance.
fn kind(&self) -> &K
Returns the kind of this error.
fn concrete_cause<T>(&self) -> Option<&T> where
T: Error + 'static,
T: Error + 'static,
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<Failure> for TrackableError<Failed>
[src]
impl<K: Debug> Debug for TrackableError<K>
[src]
impl<K: Clone> Clone for TrackableError<K>
[src]
fn clone(&self) -> TrackableError<K>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<K: ErrorKind> From<K> for TrackableError<K>
[src]
fn from(kind: K) -> Self
Performs the conversion.
impl<K: ErrorKind + Default> Default for TrackableError<K>
[src]
impl<K: ErrorKind> Display for TrackableError<K>
[src]
impl<K: ErrorKind> Error for TrackableError<K>
[src]
fn description(&self) -> &str
A short description of the error. Read more
fn cause(&self) -> Option<&Error>
The lower-level cause of this error, if any. Read more
impl<K> Trackable for TrackableError<K>
[src]
type Event = Event
Event type which a history of an instance of this type can have.
fn assign_tracking_number(&mut self)
Assigns a randomly generated tracking number to this instance. Read more
fn tracking_number(&self) -> Option<TrackingNumber>
Returns the tracking number of this instance if it has been assigned.
fn enable_tracking(self) -> Self where
Self: Sized,
Self: Sized,
Enables tracking of this instance.
fn disable_tracking(self) -> Self where
Self: Sized,
Self: Sized,
Disables tracking of this intance.
fn history(&self) -> Option<&History>
Returns the reference of the tracking history of this instance.
fn history_mut(&mut self) -> Option<&mut History>
Returns the mutable reference of the tracking history of this instance.
fn track<F>(&mut self, f: F) where
F: FnOnce() -> Self::Event,
F: FnOnce() -> Self::Event,
Add an event into the tail of the history of this instance. Read more
fn in_tracking(&self) -> bool
Returns true
if tracking of this instance is enabled, otherwise false
.