Skip to content

Commit

Permalink
feat: use anyhow for error handling of commands
Browse files Browse the repository at this point in the history
Using anyhow it should be easier to find out what was wrong or improve the error message shown to the user.
  • Loading branch information
hdevalke committed Aug 28, 2022
1 parent 5c485ae commit 51abd51
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 13 deletions.
70 changes: 70 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ include = [
]

[dependencies]
anyhow = { version = "1.0.62", features = ["backtrace"] }
clap = { version = "3.2.6", features = ["derive"] }
ctrlc = "3.2.2"
dialoguer = "0.10.1"
Expand Down
4 changes: 2 additions & 2 deletions src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{conventional::Config, Error};
use crate::conventional::Config;

mod changelog;
mod check;
mod commit;
mod version;

pub(crate) trait Command {
fn exec(&self, config: Config) -> Result<(), Error>;
fn exec(&self, config: Config) -> anyhow::Result<()>;
}
13 changes: 9 additions & 4 deletions src/cmd/changelog.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{cmp::Ordering, collections::HashMap, io::Write, path::PathBuf, str::FromStr};

use anyhow::Context as _;
use git2::Time;
use time::Date;

Expand Down Expand Up @@ -260,7 +261,7 @@ impl<'a> ChangeLogTransformer<'a> {
}

impl ChangelogCommand {
pub(crate) fn write(&self, mut config: Config, stdout: impl Write) -> Result<(), Error> {
pub(crate) fn write(&self, mut config: Config, stdout: impl Write) -> anyhow::Result<()> {
if self.no_links {
config.link_references = false;
config.link_compare = false;
Expand Down Expand Up @@ -293,7 +294,10 @@ impl ChangelogCommand {

let transformer =
ChangeLogTransformer::new(&config, self.include_hidden_sections, &helper, &self.paths)?;
match helper.find_last_version(rev)? {
match helper
.find_last_version(rev)
.with_context(|| format!("Could not find the last version for revision {rev}"))?
{
Some(last_version) => {
let semver = SemVer::from_str(rev.trim_start_matches(&self.prefix));
let from_rev = if let Ok(ref semver) = &semver {
Expand Down Expand Up @@ -354,8 +358,9 @@ impl ChangelogCommand {
}

impl Command for ChangelogCommand {
fn exec(&self, config: Config) -> Result<(), Error> {
fn exec(&self, config: Config) -> anyhow::Result<()> {
let stdout = std::io::stdout().lock();
self.write(config, stdout)
self.write(config, stdout)?;
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/cmd/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn print_check(commit: &Commit<'_>, parser: &conventional::CommitParser, types:
}

impl Command for CheckCommand {
fn exec(&self, mut config: Config) -> Result<(), Error> {
fn exec(&self, mut config: Config) -> anyhow::Result<()> {
if self.merges {
config.merges = true;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ impl Command for CheckCommand {
Ok(())
} else {
println!("\n{}/{} failed", fail, total);
Err(Error::Check)
Err(Error::Check)?
}
}
}
2 changes: 1 addition & 1 deletion src/cmd/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl Dialog {
}

impl Command for CommitCommand {
fn exec(&self, config: Config) -> Result<(), Error> {
fn exec(&self, config: Config) -> anyhow::Result<()> {
let r#type = match (
self.feat,
self.fix,
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl VersionCommand {
}

impl Command for VersionCommand {
fn exec(&self, config: Config) -> Result<(), Error> {
fn exec(&self, config: Config) -> anyhow::Result<()> {
let (version, label) = self.get_version(config.scope_regex)?;
if self.label {
println!("{label}");
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ fn main() -> Result<(), Error> {
};
match res {
Err(e) => {
match e {
Error::Check => (),
match e.downcast_ref::<Error>() {
Some(Error::Check) => (),
_ => {
eprintln!("{}", e);
eprintln!("{:?}", e);
}
}
exit(1)
Expand Down

0 comments on commit 51abd51

Please sign in to comment.