Skip to content

Instantly share code, notes, and snippets.

@kenyipp
Created December 17, 2022 21:45
Show Gist options
  • Save kenyipp/b749bbf896353e82a5e2602f2f867cd2 to your computer and use it in GitHub Desktop.
Save kenyipp/b749bbf896353e82a5e2602f2f867cd2 to your computer and use it in GitHub Desktop.
Handle the rust errors using the anyhow and thiserror crates
use anyhow;
use anyhow::Result;
use thiserror::Error;
#[tokio::main]
async fn main() -> Result<()> {
match always_error() {
Ok(()) => println!("Success!"),
Err(err) => println!("The error message is: {}", err),
}
Ok(())
}
fn always_error() -> Result<()> {
Err((Errors::Generic { message: String::from("generic error message") }).into())
}
#[derive(Error, Debug)]
pub enum Errors {
#[error("{:?}", message)] Generic {
message: String,
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment