diff --git a/scarb/src/bin/scarb/commands/fmt.rs b/scarb/src/bin/scarb/commands/fmt.rs index bbd1c3509..838d66342 100644 --- a/scarb/src/bin/scarb/commands/fmt.rs +++ b/scarb/src/bin/scarb/commands/fmt.rs @@ -1,21 +1,23 @@ use anyhow::Result; use crate::args::FmtArgs; +use crate::errors::ErrorWithExitCode; use scarb::core::Config; use scarb::ops; #[tracing::instrument(skip_all, level = "info")] pub fn run(args: FmtArgs, config: &Config) -> Result<()> { let ws = ops::read_workspace(config.manifest_path(), config)?; - match ops::format( + if ops::format( ops::FmtOptions { check: args.check, pkg_name: args.package, color: !args.no_color, }, &ws, - ) { - Ok(true) => Ok(()), - _ => std::process::exit(1), + )? { + Ok(()) + } else { + Err(ErrorWithExitCode::code(1).into()) } } diff --git a/scarb/src/bin/scarb/errors.rs b/scarb/src/bin/scarb/errors.rs index 0af5f9e35..443eaff3b 100644 --- a/scarb/src/bin/scarb/errors.rs +++ b/scarb/src/bin/scarb/errors.rs @@ -1,8 +1,5 @@ -use anyhow::Result; use thiserror::Error; -pub type ScarbResult = Result; - /// The ErrorWithExitCode is the error type used at Scarb's CLI-layer. #[derive(Error, Debug)] #[error("ErrorWithExitCode exit_code: {exit_code}")] diff --git a/scarb/src/bin/scarb/main.rs b/scarb/src/bin/scarb/main.rs index 898b30337..a46225989 100644 --- a/scarb/src/bin/scarb/main.rs +++ b/scarb/src/bin/scarb/main.rs @@ -1,10 +1,12 @@ use std::env; -use anyhow::Result; +use anyhow::{Error, Result}; use clap::Parser; +use tracing::debug; use tracing_log::AsTrace; use tracing_subscriber::EnvFilter; +use crate::errors::ErrorWithExitCode; use args::ScarbArgs; use scarb::core::Config; use scarb::ops; @@ -12,6 +14,7 @@ use scarb::ui::Ui; mod args; mod commands; +mod errors; fn main() { let args = ScarbArgs::parse(); @@ -30,6 +33,20 @@ fn main() { let ui = Ui::new(args.ui_verbosity(), args.output_format()); if let Err(err) = cli_main(args) { + exit_with_error(err, &ui); + } +} + +fn exit_with_error(err: Error, ui: &Ui) { + debug!("exit_with_error; err={:?}", err); + + if let Some(error_with_exit_code) = err.downcast_ref::() { + let ErrorWithExitCode { source, exit_code } = error_with_exit_code; + if let Some(source_err) = source { + ui.anyhow(source_err); + } + std::process::exit(*exit_code); + } else { ui.anyhow(&err); std::process::exit(1); }