From 049e46fbbba958c6b71a423bfe600e56a693e6f2 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 31 Jan 2023 21:22:37 +0000 Subject: [PATCH] Display the causes of an error (#931) * WIP try and figure out context errors * Impl Debug for ErrorVariant * Use Debug in format_err * Add manifest path context for crate_metadata.rs --- crates/build/src/crate_metadata.rs | 7 ++++++- crates/cargo-contract/src/cmd/extrinsics/error.rs | 14 ++++++++++++-- crates/cargo-contract/src/main.rs | 8 ++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/crates/build/src/crate_metadata.rs b/crates/build/src/crate_metadata.rs index 14e5cd5e3..d7d2257cb 100644 --- a/crates/build/src/crate_metadata.rs +++ b/crates/build/src/crate_metadata.rs @@ -168,7 +168,12 @@ fn get_cargo_metadata(manifest_path: &ManifestPath) -> Result<(CargoMetadata, Pa let metadata = cmd .manifest_path(manifest_path.as_ref()) .exec() - .context("Error invoking `cargo metadata`")?; + .with_context(|| { + format!( + "Error invoking `cargo metadata` for {}", + manifest_path.as_ref().display() + ) + })?; let root_package_id = metadata .resolve .as_ref() diff --git a/crates/cargo-contract/src/cmd/extrinsics/error.rs b/crates/cargo-contract/src/cmd/extrinsics/error.rs index 417b0fe8c..dd6e253a0 100644 --- a/crates/cargo-contract/src/cmd/extrinsics/error.rs +++ b/crates/cargo-contract/src/cmd/extrinsics/error.rs @@ -15,7 +15,11 @@ // along with cargo-contract. If not, see . use sp_runtime::DispatchError; -use std::fmt::Display; +use std::fmt::{ + self, + Debug, + Display, +}; #[derive(serde::Serialize)] pub enum ErrorVariant { @@ -93,8 +97,14 @@ impl ErrorVariant { } } +impl Debug for ErrorVariant { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + ::fmt(self, f) + } +} + impl Display for ErrorVariant { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { ErrorVariant::Module(err) => { f.write_fmt(format_args!( diff --git a/crates/cargo-contract/src/main.rs b/crates/cargo-contract/src/main.rs index 70c0d1ff3..977de648f 100644 --- a/crates/cargo-contract/src/main.rs +++ b/crates/cargo-contract/src/main.rs @@ -34,7 +34,7 @@ use contract_build::{ OutputType, }; use std::{ - fmt::Display, + fmt::Debug, path::PathBuf, str::FromStr, }; @@ -136,7 +136,7 @@ fn main() { match exec(args.cmd) { Ok(()) => {} Err(err) => { - eprintln!("{err}"); + eprintln!("{err:?}"); std::process::exit(1); } } @@ -207,10 +207,10 @@ fn map_extrinsic_err(err: ErrorVariant, is_json: bool) -> Error { } } -fn format_err(err: E) -> Error { +fn format_err(err: E) -> Error { anyhow!( "{} {}", "ERROR:".bright_red().bold(), - format!("{err}").bright_red() + format!("{err:?}").bright_red() ) }