pub struct TrackableError<K> { /* private fields */ }
Expand description
Trackable error.
§Examples
Defines your own Error
type.
#[macro_use]
extern crate trackable;
use trackable::error::{TrackableError, ErrorKind, ErrorKindExt};
#[derive(Debug, TrackableError)]
#[trackable(error_kind = "MyErrorKind")]
struct MyError(TrackableError<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`!
"#);
}
Implementations§
Source§impl<K: ErrorKind> TrackableError<K>
impl<K: ErrorKind> TrackableError<K>
Sourcepub fn concrete_cause<T>(&self) -> Option<&T>where
T: Error + 'static,
pub fn concrete_cause<T>(&self) -> Option<&T>where
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§
Source§impl<K: Clone> Clone for TrackableError<K>
impl<K: Clone> Clone for TrackableError<K>
Source§fn clone(&self) -> TrackableError<K>
fn clone(&self) -> TrackableError<K>
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<K: Debug> Debug for TrackableError<K>
impl<K: Debug> Debug for TrackableError<K>
Source§impl<'de, K> Deserialize<'de> for TrackableError<K>where
K: Deserialize<'de>,
impl<'de, K> Deserialize<'de> for TrackableError<K>where
K: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<K: ErrorKind> Display for TrackableError<K>
impl<K: ErrorKind> Display for TrackableError<K>
Source§impl<K: ErrorKind> Error for TrackableError<K>
impl<K: ErrorKind> Error for TrackableError<K>
Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§fn cause(&self) -> Option<&dyn Error>
fn cause(&self) -> Option<&dyn Error>
👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§impl<K: ErrorKind> From<K> for TrackableError<K>
impl<K: ErrorKind> From<K> for TrackableError<K>
Source§impl From<TrackableError<ErrorKind>> for IoError
impl From<TrackableError<ErrorKind>> for IoError
Source§fn from(f: TrackableError<ErrorKind>) -> Self
fn from(f: TrackableError<ErrorKind>) -> Self
Converts to this type from the input type.
Source§impl From<TrackableError<Failed>> for Failure
impl From<TrackableError<Failed>> for Failure
Source§fn from(f: TrackableError<Failed>) -> Self
fn from(f: TrackableError<Failed>) -> Self
Converts to this type from the input type.
Source§impl<K> Serialize for TrackableError<K>where
K: Serialize,
impl<K> Serialize for TrackableError<K>where
K: Serialize,
Source§impl<K> Trackable for TrackableError<K>
impl<K> Trackable for TrackableError<K>
Source§fn history(&self) -> Option<&History>
fn history(&self) -> Option<&History>
Returns the reference of the tracking history of this instance. Read more
Source§fn history_mut(&mut self) -> Option<&mut History>
fn history_mut(&mut self) -> Option<&mut History>
Returns the mutable reference of the tracking history of this instance. Read more
Source§fn track<F>(&mut self, f: F)
fn track<F>(&mut self, f: F)
Add an event into the tail of the history of this instance. Read more
Source§fn in_tracking(&self) -> bool
fn in_tracking(&self) -> bool
Returns
true
if it is being tracked, otherwise false
.Auto Trait Implementations§
impl<K> Freeze for TrackableError<K>where
K: Freeze,
impl<K> !RefUnwindSafe for TrackableError<K>
impl<K> Send for TrackableError<K>where
K: Send,
impl<K> Sync for TrackableError<K>where
K: Sync,
impl<K> Unpin for TrackableError<K>where
K: Unpin,
impl<K> !UnwindSafe for TrackableError<K>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more