diff options
author | Pascal Hertleif <[email protected]> | 2019-11-13 19:51:57 +0000 |
---|---|---|
committer | Pascal Hertleif <[email protected]> | 2019-11-13 19:51:57 +0000 |
commit | 5075c77957e127be51e1b5271567f906abeb50c6 (patch) | |
tree | c8e8d8f29011ad2b2472a201561f3f3dc1627d8f /xtask/src/lib.rs | |
parent | 5e3c1c2b5f63e57f98a7d02f75a559d225341b1c (diff) |
Use anyhow::Result in xtask, add contexts
This builds on #2231 but was actually done before that. You see, the
cause for #2231 was that I got this error message:
Error: Error { kind: Io(Os { code: 2, kind: NotFound, message: "No such file or directory" }) }
Just switching to `anyhow::Result` got me stack traces (when setting
`RUST_LIB_BACKTRACE=1`) that at least showed
stack backtrace:
0: std::backtrace::Backtrace::create
1: std::backtrace::Backtrace::capture
2: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from
3: xtask::install_server
4: xtask::install
5: xtask::main
6: std::rt::lang_start::{{closure}}
7: std::panicking::try::do_call
8: __rust_maybe_catch_panic
9: std::rt::lang_start_internal
10: std::rt::lang_start
11: main
With the added contexts (not at all exhaustive), the error became
Error: install server
Caused by:
0: build AutoCfg with target directory
1: No such file or directory (os error 2)
Since anyhow is such a small thing (no new transitive dependencies!),
and in general gives you `Result<T, Box<dyn Error>>` on steroids, I
think this a nice small change. The only slightly annoying thing was to
replace all the `Err(format!(…))?` calls (haven't even looked at whether
we can make it support wrapping strings though), but the `bail!` macro
is shorter anyway :)
Diffstat (limited to 'xtask/src/lib.rs')
-rw-r--r-- | xtask/src/lib.rs | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index bae4c4650..bfee2f9c8 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs | |||
@@ -2,10 +2,10 @@ | |||
2 | 2 | ||
3 | pub mod codegen; | 3 | pub mod codegen; |
4 | 4 | ||
5 | use anyhow::Context; | ||
6 | pub use anyhow::Result; | ||
5 | use std::{ | 7 | use std::{ |
6 | env, | 8 | env, fs, |
7 | error::Error, | ||
8 | fs, | ||
9 | io::{Error as IoError, ErrorKind}, | 9 | io::{Error as IoError, ErrorKind}, |
10 | path::{Path, PathBuf}, | 10 | path::{Path, PathBuf}, |
11 | process::{Command, Output, Stdio}, | 11 | process::{Command, Output, Stdio}, |
@@ -13,8 +13,6 @@ use std::{ | |||
13 | 13 | ||
14 | use crate::codegen::Mode; | 14 | use crate::codegen::Mode; |
15 | 15 | ||
16 | pub type Result<T> = std::result::Result<T, Box<dyn Error>>; | ||
17 | |||
18 | const TOOLCHAIN: &str = "stable"; | 16 | const TOOLCHAIN: &str = "stable"; |
19 | 17 | ||
20 | pub fn project_root() -> PathBuf { | 18 | pub fn project_root() -> PathBuf { |
@@ -69,7 +67,7 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> { | |||
69 | .status() | 67 | .status() |
70 | { | 68 | { |
71 | Ok(status) if status.success() => (), | 69 | Ok(status) if status.success() => (), |
72 | _ => install_rustfmt()?, | 70 | _ => install_rustfmt().context("install rustfmt")?, |
73 | }; | 71 | }; |
74 | 72 | ||
75 | if mode == Mode::Verify { | 73 | if mode == Mode::Verify { |
@@ -112,7 +110,7 @@ pub fn run_clippy() -> Result<()> { | |||
112 | .status() | 110 | .status() |
113 | { | 111 | { |
114 | Ok(status) if status.success() => (), | 112 | Ok(status) if status.success() => (), |
115 | _ => install_clippy()?, | 113 | _ => install_clippy().context("install clippy")?, |
116 | }; | 114 | }; |
117 | 115 | ||
118 | let allowed_lints = [ | 116 | let allowed_lints = [ |
@@ -162,9 +160,9 @@ where | |||
162 | let exec = args.next().unwrap(); | 160 | let exec = args.next().unwrap(); |
163 | let mut cmd = Command::new(exec); | 161 | let mut cmd = Command::new(exec); |
164 | f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit())); | 162 | f(cmd.args(args).current_dir(proj_dir).stderr(Stdio::inherit())); |
165 | let output = cmd.output()?; | 163 | let output = cmd.output().with_context(|| format!("running `{}`", cmdline))?; |
166 | if !output.status.success() { | 164 | if !output.status.success() { |
167 | Err(format!("`{}` exited with {}", cmdline, output.status))?; | 165 | anyhow::bail!("`{}` exited with {}", cmdline, output.status); |
168 | } | 166 | } |
169 | Ok(output) | 167 | Ok(output) |
170 | } | 168 | } |