Created
December 17, 2022 21:45
-
-
Save kenyipp/b749bbf896353e82a5e2602f2f867cd2 to your computer and use it in GitHub Desktop.
Handle the rust errors using the anyhow and thiserror crates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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