Struct trackable::error::TrackableError [−][src]
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
impl<K: ErrorKind> TrackableError<K>
[src]
pub fn new<E>(kind: K, cause: E) -> Self where
E: Into<BoxError>,
[src]
E: Into<BoxError>,
Makes a new TrackableError
instance.
pub fn kind(&self) -> &K
[src]
Returns the kind of this error.
pub fn concrete_cause<T>(&self) -> Option<&T> where
T: Error + 'static,
[src]
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<K: Clone> Clone for TrackableError<K>
[src]
fn clone(&self) -> TrackableError<K>
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<K: Debug> Debug for TrackableError<K>
[src]
impl<K: ErrorKind + Default> Default for TrackableError<K>
[src]
impl<'de, K> Deserialize<'de> for TrackableError<K> where
K: Deserialize<'de>,
[src]
K: Deserialize<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
impl<K: ErrorKind> Display for TrackableError<K>
[src]
impl<K: ErrorKind> Error for TrackableError<K>
[src]
fn description(&self) -> &str
[src]
fn cause(&self) -> Option<&dyn Error>
[src]
pub fn source(&self) -> Option<&(dyn Error + 'static)>
1.30.0[src]
pub fn backtrace(&self) -> Option<&Backtrace>
[src]
impl From<Failure> for TrackableError<Failed>
[src]
impl From<IoError> for TrackableError<ErrorKind>
[src]
impl<K: ErrorKind> From<K> for TrackableError<K>
[src]
impl From<TrackableError<ErrorKind>> for IoError
[src]
fn from(f: TrackableError<ErrorKind>) -> Self
[src]
impl From<TrackableError<Failed>> for Failure
[src]
fn from(f: TrackableError<Failed>) -> Self
[src]
impl<K> Serialize for TrackableError<K> where
K: Serialize,
[src]
K: Serialize,
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<K> Trackable for TrackableError<K>
[src]
Auto Trait Implementations
impl<K> !RefUnwindSafe for TrackableError<K>
impl<K> Send for TrackableError<K> where
K: Send,
K: Send,
impl<K> Sync for TrackableError<K> where
K: Sync,
K: Sync,
impl<K> Unpin for TrackableError<K> where
K: Unpin,
K: Unpin,
impl<K> !UnwindSafe for TrackableError<K>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,