Skip to content

Commit

Permalink
Display the causes of an error (#931)
Browse files Browse the repository at this point in the history
* WIP try and figure out context errors

* Impl Debug for ErrorVariant

* Use Debug in format_err

* Add manifest path context for crate_metadata.rs
  • Loading branch information
ascjones authored Jan 31, 2023
1 parent ee002d4 commit 049e46f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 6 additions & 1 deletion crates/build/src/crate_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 12 additions & 2 deletions crates/cargo-contract/src/cmd/extrinsics/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use sp_runtime::DispatchError;
use std::fmt::Display;
use std::fmt::{
self,
Debug,
Display,
};

#[derive(serde::Serialize)]
pub enum ErrorVariant {
Expand Down Expand Up @@ -93,8 +97,14 @@ impl ErrorVariant {
}
}

impl Debug for ErrorVariant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
<Self as Display>::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!(
Expand Down
8 changes: 4 additions & 4 deletions crates/cargo-contract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use contract_build::{
OutputType,
};
use std::{
fmt::Display,
fmt::Debug,
path::PathBuf,
str::FromStr,
};
Expand Down Expand Up @@ -136,7 +136,7 @@ fn main() {
match exec(args.cmd) {
Ok(()) => {}
Err(err) => {
eprintln!("{err}");
eprintln!("{err:?}");
std::process::exit(1);
}
}
Expand Down Expand Up @@ -207,10 +207,10 @@ fn map_extrinsic_err(err: ErrorVariant, is_json: bool) -> Error {
}
}

fn format_err<E: Display>(err: E) -> Error {
fn format_err<E: Debug>(err: E) -> Error {
anyhow!(
"{} {}",
"ERROR:".bright_red().bold(),
format!("{err}").bright_red()
format!("{err:?}").bright_red()
)
}

0 comments on commit 049e46f

Please sign in to comment.