Skip to content

Commit

Permalink
Replace deprecated error_chain crate with anyhow (dandavison#1405)
Browse files Browse the repository at this point in the history
The `error_chain` crate is now deprecated for a long time and `anyhow`
has proven to be a popular replacement for applications.

This also improves the current error messages for panics.

```
PAGER='"less' git show
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: \
Error(Msg("Could not parse pager command."), State { next_error: Some(ParseError), \
backtrace: InternalBacktrace })', src/main.rs:136:88
```

```
PAGER='"less' git show
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Could not parse pager command.

Caused by:
    missing closing quote', src/main.rs:125:88
```
  • Loading branch information
nickelc authored Apr 26, 2023
1 parent 57e7ce5 commit ce41a39
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 26 deletions.
17 changes: 7 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ chrono = "0.4.23"
chrono-humanize = "0.2.2"
ansi_colours = "1.2.1"
ansi_term = "0.12.1"
anyhow = "1.0.70"
atty = "0.2.14"
bitflags = "2.2.1"
box_drawing = "0.1.2"
Expand Down Expand Up @@ -55,10 +56,5 @@ version = "0.28.2"
default-features = false
features = []

[dependencies.error-chain]
version = "0.12.4"
default-features = false
features = []

[profile.test]
opt-level = 2
2 changes: 1 addition & 1 deletion src/git_config/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl FromStr for GitRemoteRepo {
),
})
} else {
Err("Not a GitHub, GitLab, SourceHut or Codeberg repo.".into())
Err(anyhow!("Not a GitHub, GitLab, SourceHut or Codeberg repo."))
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,7 @@ where
}

pub mod errors {
error_chain::error_chain! {
foreign_links {
Io(::std::io::Error);
SyntectError(::syntect::LoadingError);
ParseIntError(::std::num::ParseIntError);
}
}
pub use anyhow::{anyhow, Context, Error, Result};
}

#[cfg(not(tarpaulin_include))]
Expand Down
5 changes: 2 additions & 3 deletions src/utils/bat/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ impl OutputType {
.or(pager_from_env)
.unwrap_or_else(|| String::from("less"));

let pagerflags =
shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
let pagerflags = shell_words::split(&pager).context("Could not parse pager command.")?;

Ok(match pagerflags.split_first() {
Some((pager_name, args)) => {
Expand Down Expand Up @@ -117,7 +116,7 @@ impl OutputType {
OutputType::Pager(ref mut command) => command
.stdin
.as_mut()
.chain_err(|| "Could not open stdin for pager")?,
.context("Could not open stdin for pager")?,
OutputType::Stdout(ref mut handle) => handle,
})
}
Expand Down

0 comments on commit ce41a39

Please sign in to comment.