From 6cb4e3fe3b3dccc4269ab6d34c8b377ecc276ed3 Mon Sep 17 00:00:00 2001 From: MOZGIII Date: Sat, 12 Nov 2022 16:31:30 +0400 Subject: [PATCH] Pretty errors (#9) --- Cargo.lock | 158 ++++++++++++++++++++++++++++++- crates/cli/Cargo.toml | 3 +- crates/cli/src/main.rs | 30 ++++-- crates/cli/src/package_render.rs | 4 +- 4 files changed, 178 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d9fead..9e35833 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,10 +3,19 @@ version = 3 [[package]] -name = "anyhow" -version = "1.0.66" +name = "addr2line" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" +checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] name = "atty" @@ -25,6 +34,21 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "backtrace" +version = "0.3.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + [[package]] name = "base64" version = "0.13.1" @@ -107,6 +131,33 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "color-eyre" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -161,6 +212,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "eyre" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "fastrand" version = "1.8.0" @@ -299,6 +360,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "gimli" +version = "0.26.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d" + [[package]] name = "h2" version = "0.3.15" @@ -383,8 +450,9 @@ checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" name = "humanode-distribution" version = "0.1.0" dependencies = [ - "anyhow", "clap", + "color-eyre", + "eyre", "futures", "humanode-distribution-detection", "humanode-distribution-installer", @@ -490,6 +558,12 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + [[package]] name = "indexmap" version = "1.9.1" @@ -563,6 +637,15 @@ version = "0.3.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" +[[package]] +name = "miniz_oxide" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34" +dependencies = [ + "adler", +] + [[package]] name = "mio" version = "0.8.5" @@ -603,6 +686,15 @@ dependencies = [ "libc", ] +[[package]] +name = "object" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21158b2c33aa6d4561f1c0a6ea283ca92bc54802a93b263e910746d679a7eb53" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.16.0" @@ -660,6 +752,12 @@ version = "6.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3baf96e39c5359d2eb0dd6ccb42c62b91d9678aa68160d261b9e0ccbf9e9dea9" +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + [[package]] name = "percent-encoding" version = "2.2.0" @@ -781,6 +879,12 @@ dependencies = [ "winreg", ] +[[package]] +name = "rustc-demangle" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" + [[package]] name = "ryu" version = "1.0.11" @@ -887,6 +991,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + [[package]] name = "slab" version = "0.4.7" @@ -966,6 +1079,15 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" +dependencies = [ + "once_cell", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1058,6 +1180,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6176eae26dd70d0c919749377897b54a9276bd7061339665dd68777926b5a70" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", ] [[package]] @@ -1110,6 +1254,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 2da97d9..85a87d3 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -9,8 +9,9 @@ humanode-distribution-installer = { path = "../installer" } humanode-distribution-resolver = { path = "../resolver" } humanode-distribution-schema = { path = "../schema" } -anyhow = "1" clap = { version = "4", features = ["derive"] } +color-eyre = "0.6" +eyre = "0.6" futures = "0.3" reqwest = "0.11" serde = "1.0" diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index a9f2a5e..ae2a976 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3,6 +3,8 @@ #![allow(missing_docs)] #![allow(clippy::missing_docs_in_private_items)] +use std::process::ExitCode; + use clap::{Args, Parser, Subcommand}; use humanode_distribution_resolver::resolve::Contextualized; use humanode_distribution_schema::manifest::Package; @@ -91,20 +93,28 @@ struct Install { } #[tokio::main] -async fn main() { +async fn main() -> ExitCode { + color_eyre::install().unwrap(); let cli = Cli::parse(); - match cli.command { - Command::List(args) => list(args).await.unwrap(), - Command::Eval(args) => eval(args).await.unwrap(), - Command::Install(args) => install(args).await.unwrap(), + let result = match cli.command { + Command::List(args) => list(args).await, + Command::Eval(args) => eval(args).await, + Command::Install(args) => install(args).await, + }; + + if let Err(error) = result { + eprintln!("Error: {error:?}"); + return ExitCode::FAILURE; } + + ExitCode::SUCCESS } /// Common CLI logic to run the resolver from the given args. async fn resolve( resolution_args: ResolutionArgs, -) -> Result>, anyhow::Error> { +) -> Result>, eyre::Error> { let ResolutionArgs { repo_urls, manifest_urls, @@ -145,7 +155,7 @@ async fn resolve( fn select( args: SelectionArgs, packages: Vec>, -) -> Result, anyhow::Error> { +) -> Result, eyre::Error> { let SelectionArgs { package_display_name, } = args; @@ -159,7 +169,7 @@ fn select( } /// List command. -async fn list(args: List) -> Result<(), anyhow::Error> { +async fn list(args: List) -> Result<(), eyre::Error> { let List { resolution_args, rendering_args, @@ -175,7 +185,7 @@ async fn list(args: List) -> Result<(), anyhow::Error> { } /// Eval command. -async fn eval(args: Eval) -> Result<(), anyhow::Error> { +async fn eval(args: Eval) -> Result<(), eyre::Error> { let Eval { resolution_args, selection_args, @@ -189,7 +199,7 @@ async fn eval(args: Eval) -> Result<(), anyhow::Error> { } /// Install command. -async fn install(args: Install) -> Result<(), anyhow::Error> { +async fn install(args: Install) -> Result<(), eyre::Error> { let Install { resolution_args, selection_args, diff --git a/crates/cli/src/package_render.rs b/crates/cli/src/package_render.rs index 619d1a0..21c49d9 100644 --- a/crates/cli/src/package_render.rs +++ b/crates/cli/src/package_render.rs @@ -19,7 +19,7 @@ impl Renderer { &self, mut writer: impl std::io::Write, package: &Package, - ) -> Result<(), anyhow::Error> { + ) -> Result<(), eyre::Error> { match self { Self::DisplayName => writer .write_all(package.display_name.as_bytes()) @@ -33,7 +33,7 @@ impl Renderer { } /// Render the package into a string according to the renderer config. - pub fn render_to_string(&self, package: &Package) -> Result { + pub fn render_to_string(&self, package: &Package) -> Result { let mut buf = Vec::new(); self.render(&mut buf, package)?; Ok(String::from_utf8(buf)?)