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 :)
This commit is contained in:
Pascal Hertleif 2019-11-13 20:51:57 +01:00
parent 5e3c1c2b5f
commit 5075c77957
7 changed files with 36 additions and 29 deletions

View file

@ -9,6 +9,7 @@
//! `.cargo/config`.
mod help;
use anyhow::Context;
use autocfg;
use core::fmt::Write;
use core::str;
@ -114,21 +115,21 @@ fn handle_extra_flags(e: pico_args::Error) -> Result<()> {
write!(&mut invalid_flags, "{}, ", flag)?;
}
let (invalid_flags, _) = invalid_flags.split_at(invalid_flags.len() - 2);
Err(format!("Invalid flags: {}", invalid_flags).into())
anyhow::bail!("Invalid flags: {}", invalid_flags)
} else {
Err(e.to_string().into())
anyhow::bail!(e.to_string())
}
}
fn install(opts: InstallOpt) -> Result<()> {
if cfg!(target_os = "macos") {
fix_path_for_mac()?
fix_path_for_mac().context("Fix path for mac")?
}
if let Some(server) = opts.server {
install_server(server)?;
install_server(server).context("install server")?;
}
if let Some(client) = opts.client {
install_client(client)?;
install_client(client).context("install client")?;
}
Ok(())
}
@ -140,7 +141,7 @@ fn fix_path_for_mac() -> Result<()> {
const ROOT_DIR: &str = "";
let home_dir = match env::var("HOME") {
Ok(home) => home,
Err(e) => Err(format!("Failed getting HOME from environment with error: {}.", e))?,
Err(e) => anyhow::bail!("Failed getting HOME from environment with error: {}.", e),
};
[ROOT_DIR, &home_dir]
@ -154,12 +155,12 @@ fn fix_path_for_mac() -> Result<()> {
if !vscode_path.is_empty() {
let vars = match env::var_os("PATH") {
Some(path) => path,
None => Err("Could not get PATH variable from env.")?,
None => anyhow::bail!("Could not get PATH variable from env."),
};
let mut paths = env::split_paths(&vars).collect::<Vec<_>>();
paths.append(&mut vscode_path);
let new_paths = env::join_paths(paths)?;
let new_paths = env::join_paths(paths).context("build env PATH")?;
env::set_var("PATH", &new_paths);
}
@ -198,7 +199,7 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
let code_binary = match code_binary {
Some(it) => it,
None => Err("Can't execute `code --version`. Perhaps it is not in $PATH?")?,
None => anyhow::bail!("Can't execute `code --version`. Perhaps it is not in $PATH?"),
};
Cmd {
@ -219,8 +220,10 @@ fn install_client(ClientOpt::VsCode: ClientOpt) -> Result<()> {
.run_with_output()?;
if !str::from_utf8(&output.stdout)?.contains("ra-lsp") {
Err("Could not install the Visual Studio Code extension. \
Please make sure you have at least NodeJS 10.x installed and try again.")?;
anyhow::bail!(
"Could not install the Visual Studio Code extension. \
Please make sure you have at least NodeJS 10.x installed and try again."
);
}
Ok(())