From 2a73aa731d4353299e32f294490589652ebfdcd0 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Mon, 18 Sep 2023 21:09:58 -0400 Subject: [PATCH] Make error messages nicer (#551) * Remove unhelpful information from errors Before: ``` Git repositories failed: 0: error: cannot pull with rebase: You have unstaged changes. error: Please commit or stash them. 0: Location: src/steps/git.rs:39 Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it. Run with RUST_BACKTRACE=full to include source snippets. ``` After: ``` Git repositories failed: 0: Failed to pull /Users/wiggles/.dotfiles 1: error: cannot pull with rebase: You have unstaged changes. error: Please commit or stash them. Location: src/steps/git.rs:39 ``` * Improve git_repos errors This removes the extra blank "0:" line at the end of the error, doesn't print the error message twice, and provides the repo path in the error message. --- src/main.rs | 15 ++++++++++++++- src/steps/git.rs | 14 +++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index b5193d34..7d275eb5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ pub static XDG_DIRS: Lazy = Lazy::new(|| Xdg::new().expect("No home directo pub static WINDOWS_DIRS: Lazy = Lazy::new(|| Windows::new().expect("No home directory")); fn run() -> Result<()> { - color_eyre::install()?; + install_color_eyre()?; ctrlc::set_handler(); let opt = CommandLineArgs::parse(); @@ -571,3 +571,16 @@ fn install_tracing(filter_directives: &str) -> Result<()> { Ok(()) } + +fn install_color_eyre() -> Result<()> { + color_eyre::config::HookBuilder::new() + // Don't display the backtrace reminder by default: + // Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it. + // Run with RUST_BACKTRACE=full to include source snippets. + .display_env_section(false) + // Display location information by default: + // Location: + // src/steps.rs:92 + .display_location_section(true) + .install() +} diff --git a/src/steps/git.rs b/src/steps/git.rs index d677d367..4657447f 100644 --- a/src/steps/git.rs +++ b/src/steps/git.rs @@ -3,6 +3,7 @@ use std::io; use std::path::{Path, PathBuf}; use std::process::{Command, Output, Stdio}; +use color_eyre::eyre::Context; use color_eyre::eyre::{eyre, Result}; use console::style; use futures::stream::{iter, FuturesUnordered}; @@ -33,10 +34,12 @@ pub struct Repositories<'a> { bad_patterns: Vec, } +#[track_caller] fn output_checked_utf8(output: Output) -> Result<()> { if !(output.status.success()) { - let stderr = String::from_utf8(output.stderr).unwrap(); - Err(eyre!(stderr)) + let stderr = String::from_utf8_lossy(&output.stderr); + let stderr = stderr.trim(); + Err(eyre!("{stderr}")) } else { Ok(()) } @@ -66,11 +69,12 @@ async fn pull_repository(repo: String, git: &Path, ctx: &ExecutionContext<'_>) - .stdin(Stdio::null()) .output() .await?; - let result = output_checked_utf8(pull_output).and_then(|_| output_checked_utf8(submodule_output)); + let result = output_checked_utf8(pull_output) + .and_then(|_| output_checked_utf8(submodule_output)) + .wrap_err_with(|| format!("Failed to pull {repo}")); - if let Err(message) = &result { + if result.is_err() { println!("{} pulling {}", style("Failed").red().bold(), &repo); - print!("{message}"); } else { let after_revision = get_head_revision(git, &repo);