Skip to content

Instantly share code, notes, and snippets.

@mikeando
Created May 12, 2020 23:51
  • Select an option

Select an option

Revisions

  1. mikeando created this gist May 12, 2020.
    34 changes: 34 additions & 0 deletions thiserror_backtrace.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #![feature(backtrace)]

    extern crate thiserror;

    use thiserror::Error;
    use std::backtrace::Backtrace;

    #[derive(Error, Debug)]
    pub enum DataStoreError {
    //#[error("data store disconnected")]
    //Disconnect(#[from] io::Error),
    #[error("the data for key `{0}` is not available")]
    Redaction(String),
    #[error("invalid header (expected {expected:?}, found {found:?})")]
    InvalidHeader {
    expected: String,
    found: String,
    backtrace: Backtrace,
    },
    #[error("unknown data store error")]
    Unknown,
    }

    pub fn explode() -> Result<(),DataStoreError> {
    Err(DataStoreError::InvalidHeader { expected: "A".to_owned(), found: "B".to_owned(), backtrace: Backtrace::force_capture() })
    }

    fn main() {
    use std::error::Error;
    let e = explode().err().unwrap();
    let b = e.backtrace();
    println!("e = {}", e);
    println!("b = {:?}", b);
    }