Skip to content

Instantly share code, notes, and snippets.

@mexus
Created April 14, 2018 09:28
Show Gist options
  • Save mexus/c37243db0c9d3c8d5880b70c8be2151e to your computer and use it in GitHub Desktop.
Save mexus/c37243db0c9d3c8d5880b70c8be2151e to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate failure;
use failure::Error;
#[derive(Fail, Debug)]
enum MyEnumError {
#[fail(display = "An error occurred.")]
Variant1,
#[fail(display = "A different error occurred.")]
Variant2(#[cause] ::std::io::Error),
}
fn err1() -> Result<(), Error> {
bail!(MyEnumError::Variant1)
}
impl From<::std::io::Error> for MyEnumError {
fn from(e: ::std::io::Error) -> Self {
MyEnumError::Variant2(e)
}
}
fn io_err() -> Result<(), MyEnumError> {
let _ = std::fs::File::open("/lolwut")?;
Ok(())
}
fn io_err2() -> Result<(), Error> {
let _ = std::fs::File::open("/lolwut")?;
Ok(())
}
fn main() -> Result<(), Error> {
let _ = err1()?;
let _ = io_err()?;
let _ = io_err2()?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment