From 5fbb8a381c557ab6ea357892a4b8b4b4b97a0a63 Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Tue, 27 Oct 2020 22:31:57 +0100 Subject: [PATCH] use color_eyre for its backtrace feature and error reporting (#1855) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * use color_eyre for its backtrace feature and error reporting * lost Cargo.lock update * wrapper * add error glue to be able to utilize eyre * Update src/main.rs Co-authored-by: Bastian Köcher * Update src/main.rs Co-authored-by: Bastian Köcher * avoid duplicate error print * Update src/main.rs Co-authored-by: Andronik Ordian Co-authored-by: Bastian Köcher Co-authored-by: Andronik Ordian --- polkadot/Cargo.lock | 59 ++++++++++++++++++++++++++++++++++++++++++++ polkadot/Cargo.toml | 6 +++++ polkadot/src/main.rs | 40 ++++++++++++++++++++++++++++-- 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/polkadot/Cargo.lock b/polkadot/Cargo.lock index a5637f921b..5f4a8d3aac 100644 --- a/polkadot/Cargo.lock +++ b/polkadot/Cargo.lock @@ -752,6 +752,32 @@ dependencies = [ "bitflags", ] +[[package]] +name = "color-eyre" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2a5123db5af8349c41c43ed0e5dca1cd56c911ea0c4ce6e6ff30f159fa5d27e" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell 1.4.1", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a99aa4aa18448eef4c7d3f86d2720d2d8cad5c860fe9ff9b279293efdc8f5be" +dependencies = [ + "ansi_term 0.11.0", + "tracing-core", + "tracing-error", +] + [[package]] name = "concurrent-queue" version = "1.2.2" @@ -1297,6 +1323,16 @@ dependencies = [ "futures 0.3.5", ] +[[package]] +name = "eyre" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c5cb4dc433c59f09df4b4450f649cbfed61e8a3505abe32e4154066439157e" +dependencies = [ + "indenter", + "once_cell 1.4.1", +] + [[package]] name = "failure" version = "0.1.8" @@ -2315,6 +2351,12 @@ dependencies = [ "syn 1.0.33", ] +[[package]] +name = "indenter" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0bd112d44d9d870a6819eb505d04dd92b5e4d94bb8c304924a0872ae7016fb5" + [[package]] name = "indexmap" version = "1.4.0" @@ -3767,6 +3809,12 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "owo-colors" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a1250cdd103eef6bd542b5ae82989f931fc00a41a27f60377338241594410f3" + [[package]] name = "pallet-authority-discovery" version = "2.0.0" @@ -4700,6 +4748,7 @@ name = "polkadot" version = "0.8.26" dependencies = [ "assert_cmd", + "color-eyre", "futures 0.3.5", "nix 0.17.0", "parity-util-mem", @@ -9251,6 +9300,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "tracing-error" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4d7c0b83d4a500748fa5879461652b361edf5c9d51ede2a2ac03875ca185e24" +dependencies = [ + "tracing", + "tracing-subscriber", +] + [[package]] name = "tracing-futures" version = "0.2.4" diff --git a/polkadot/Cargo.toml b/polkadot/Cargo.toml index edd8f8a113..ad0406e000 100644 --- a/polkadot/Cargo.toml +++ b/polkadot/Cargo.toml @@ -13,6 +13,7 @@ readme = "README.md" [dependencies] cli = { package = "polkadot-cli", path = "cli" } +color-eyre = "0.5.6" futures = "0.3.4" service = { package = "polkadot-service", path = "node/service" } parity-util-mem = { version = "*", default-features = false, features = ["jemalloc-global"] } @@ -72,6 +73,11 @@ members = [ [badges] maintenance = { status = "actively-developed" } +# make sure dev builds with backtrace do +# not slow us down +[profile.dev.package.backtrace] +opt-level = 3 + [profile.release] # Polkadot runtime requires unwinding. panic = "unwind" diff --git a/polkadot/src/main.rs b/polkadot/src/main.rs index 58f475471e..d26fa4e3be 100644 --- a/polkadot/src/main.rs +++ b/polkadot/src/main.rs @@ -18,6 +18,42 @@ #![warn(missing_docs)] -fn main() -> cli::Result<()> { - cli::run() +use color_eyre::eyre; + +use cli::Error as PolkaError; + +use std::{error, fmt}; + +/// A helper to satisfy the requirements of `eyre` +/// compatible errors, which require `Send + Sync` +/// which are not satisfied by the `sp_*` crates. +#[derive(Debug)] +struct ErrorWrapper(std::sync::Arc); + +// nothing is going to be sent to another thread +// it merely exists to glue two distinct error +// types together where the requirements differ +// with `Sync + Send` and without them for `wasm`. +unsafe impl Sync for ErrorWrapper {} +unsafe impl Send for ErrorWrapper {} + +impl error::Error for ErrorWrapper { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + (&*self.0).source().and_then(|e| e.source()) + } + fn description(&self) -> &str { + "Error Wrapper" + } +} + +impl fmt::Display for ErrorWrapper { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", &*self.0) + } +} + +fn main() -> eyre::Result<()> { + color_eyre::install()?; + cli::run().map_err(|e| ErrorWrapper(std::sync::Arc::new(e)))?; + Ok(()) }