Skip to content

Commit 3bf87ee

Browse files
committed
refactor(cli): Pull out run error handling
1 parent f4b9492 commit 3bf87ee

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/bin/cargo/commands/run.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
8181
}
8282
};
8383

84-
ops::run(&ws, &compile_opts, &values_os(args, "args")).map_err(|err| {
85-
let proc_err = match err.downcast_ref::<ProcessError>() {
86-
Some(e) => e,
87-
None => return CliError::new(err, 101),
88-
};
89-
90-
// If we never actually spawned the process then that sounds pretty
91-
// bad and we always want to forward that up.
92-
let exit_code = match proc_err.code {
93-
Some(exit) => exit,
94-
None => return CliError::new(err, 101),
95-
};
96-
97-
// If `-q` was passed then we suppress extra error information about
98-
// a failed process, we assume the process itself printed out enough
99-
// information about why it failed so we don't do so as well
100-
let is_quiet = config.shell().verbosity() == Verbosity::Quiet;
101-
if is_quiet {
102-
CliError::code(exit_code)
103-
} else {
104-
CliError::new(err, exit_code)
105-
}
106-
})
84+
ops::run(&ws, &compile_opts, &values_os(args, "args")).map_err(|err| to_run_error(config, err))
10785
}
10886

10987
pub fn is_manifest_command(arg: &str) -> bool {
@@ -118,3 +96,27 @@ pub fn exec_manifest_command(config: &Config, cmd: &str, _args: &[&OsStr]) -> Cl
11896

11997
todo!("support for running manifest-commands is not yet implemented")
12098
}
99+
100+
fn to_run_error(config: &cargo::util::Config, err: anyhow::Error) -> CliError {
101+
let proc_err = match err.downcast_ref::<ProcessError>() {
102+
Some(e) => e,
103+
None => return CliError::new(err, 101),
104+
};
105+
106+
// If we never actually spawned the process then that sounds pretty
107+
// bad and we always want to forward that up.
108+
let exit_code = match proc_err.code {
109+
Some(exit) => exit,
110+
None => return CliError::new(err, 101),
111+
};
112+
113+
// If `-q` was passed then we suppress extra error information about
114+
// a failed process, we assume the process itself printed out enough
115+
// information about why it failed so we don't do so as well
116+
let is_quiet = config.shell().verbosity() == Verbosity::Quiet;
117+
if is_quiet {
118+
CliError::code(exit_code)
119+
} else {
120+
CliError::new(err, exit_code)
121+
}
122+
}

0 commit comments

Comments
 (0)