Struct TrackableError

Source
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>

Source

pub fn new<E>(kind: K, cause: E) -> Self
where E: Into<BoxError>,

Makes a new TrackableError instance.

Source

pub fn kind(&self) -> &K

Returns the kind of this error.

Source

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>

Source§

fn clone(&self) -> TrackableError<K>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug> Debug for TrackableError<K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: ErrorKind + Default> Default for TrackableError<K>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K: ErrorKind> Display for TrackableError<K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K: ErrorKind> Error for TrackableError<K>

Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Failure> for TrackableError<Failed>

Source§

fn from(f: Failure) -> Self

Converts to this type from the input type.
Source§

impl From<IoError> for TrackableError<ErrorKind>

Source§

fn from(f: IoError) -> Self

Converts to this type from the input type.
Source§

impl<K: ErrorKind> From<K> for TrackableError<K>

Source§

fn from(kind: K) -> Self

Converts to this type from the input type.
Source§

impl From<TrackableError<ErrorKind>> for IoError

Source§

fn from(f: TrackableError<ErrorKind>) -> Self

Converts to this type from the input type.
Source§

impl From<TrackableError<Failed>> for Failure

Source§

fn from(f: TrackableError<Failed>) -> Self

Converts to this type from the input type.
Source§

impl<K> Serialize for TrackableError<K>
where K: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<K> Trackable for TrackableError<K>

Source§

type Event = Location

Event type which a history of an instance of this type can have.
Source§

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>

Returns the mutable reference of the tracking history of this instance. Read more
Source§

fn track<F>(&mut self, f: F)
where F: FnOnce() -> Self::Event,

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

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,