From 03c5919538732f976d57ae16b61d4dd9d5f5ecb5 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Tue, 9 Feb 2021 23:22:46 +0800 Subject: [PATCH] feat: add thiserror anyhow --- .gitignore | 1 + __err/thiserror_anyhow/Cargo.lock | 70 ++++++++++++++++++++++++++++++ __err/thiserror_anyhow/Cargo.toml | 12 +++++ __err/thiserror_anyhow/src/main.rs | 19 ++++++++ 4 files changed, 102 insertions(+) create mode 100644 __err/thiserror_anyhow/Cargo.lock create mode 100644 __err/thiserror_anyhow/Cargo.toml create mode 100644 __err/thiserror_anyhow/src/main.rs diff --git a/.gitignore b/.gitignore index f36b791..069f973 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea/ # ---> Rust # Generated by Cargo # will have compiled files and executables diff --git a/__err/thiserror_anyhow/Cargo.lock b/__err/thiserror_anyhow/Cargo.lock new file mode 100644 index 0000000..57cb24f --- /dev/null +++ b/__err/thiserror_anyhow/Cargo.lock @@ -0,0 +1,70 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "anyhow" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1" + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "thiserror" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76cc616c6abf8c8928e2fdcc0dbfab37175edd8fb49a4641066ad1364fdab146" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9be73a2caec27583d0046ef3796c3794f868a5bc813db689eed00c7631275cd1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "thiserror_anyhow" +version = "0.1.0" +dependencies = [ + "anyhow", + "thiserror", +] + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" diff --git a/__err/thiserror_anyhow/Cargo.toml b/__err/thiserror_anyhow/Cargo.toml new file mode 100644 index 0000000..95614ec --- /dev/null +++ b/__err/thiserror_anyhow/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "thiserror_anyhow" +version = "0.1.0" +authors = ["Hatter Jiang "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +thiserror = "1.0.23" +anyhow = "1.0.38" + diff --git a/__err/thiserror_anyhow/src/main.rs b/__err/thiserror_anyhow/src/main.rs new file mode 100644 index 0000000..47ec359 --- /dev/null +++ b/__err/thiserror_anyhow/src/main.rs @@ -0,0 +1,19 @@ +use thiserror::Error; +use anyhow::{Context, Result}; + +#[derive(Error, Debug)] +pub enum SimpleError { + #[error("error status")] + ErrorStatus(u32), + #[error("unknown error")] + Unknown, +} + +fn main() { + do_something().unwrap(); +} + +fn do_something() -> Result> { + + Err(Box::new(SimpleError::Unknown)).with_context(|| format!("Error unknown!"))? +}