diff --git a/Cargo.lock b/Cargo.lock index 8ae5371..6085d36 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -59,6 +59,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "anyhow" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6" + [[package]] name = "autocfg" version = "1.1.0" @@ -137,6 +143,7 @@ checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" name = "conventional_commits_next_version" version = "6.0.1" dependencies = [ + "anyhow", "clap", "git2", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index 50daa54..55b6ce8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,9 @@ lazy_static = "1.4.0" strum = "0.26.0" strum_macros = "0.26.0" +# For error handling. +anyhow = "1.0.89" + [dev-dependencies] # For parameterized testing. diff --git a/end-to-end-tests/features/steps/assertions.py b/end-to-end-tests/features/steps/assertions.py index ff66106..7bd5616 100644 --- a/end-to-end-tests/features/steps/assertions.py +++ b/end-to-end-tests/features/steps/assertions.py @@ -24,6 +24,12 @@ def assert_error_equals(result, error): f"Error = {error.encode()}.\n" +def assert_error_contains(result, error): + assert error in result.stderr, "Expected standard error to contain the error.\n" + \ + f"Standard error = {result.stderr.encode()}.\n" + \ + f"Error = {error.encode()}.\n" + + def assert_error_matches_regex(result, regex): assert regex.match(result.stderr) is not None, f"Expected standard errors to match the regex.\n" + \ f"Standard error = {result.stderr.encode()}.\n" + \ diff --git a/src/main.rs b/src/main.rs index eb4fefc..de3a0e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,13 +6,12 @@ extern crate pretty_env_logger; use std::io::{stdin, Read}; +use anyhow::{bail, Result}; use clap::Parser; use git2::Repository; -pub use crate::calculation_mode::CalculationMode; use crate::cli::Arguments; -pub use crate::commits::Commits; -pub use crate::git_history_mode::GitHistoryMode; +use crate::commits::Commits; mod calculation_mode; mod cli; @@ -27,12 +26,13 @@ fn main() { let arguments = Arguments::parse(); trace!("The command line arguments provided are {arguments:?}."); - if run(arguments).is_err() { + if let Err(err) = run(arguments) { + error!("{:?}", err); std::process::exit(ERROR_EXIT_CODE); } } -fn run(arguments: Arguments) -> Result<(), git2::Error> { +fn run(arguments: Arguments) -> Result<()> { let commits = match ( arguments.from_stdin, arguments.from_commit_hash, @@ -63,7 +63,7 @@ fn run(arguments: Arguments) -> Result<(), git2::Error> { ) } (_, _, _) => { - unreachable!("Invalid combination of arguments."); + bail!("Invalid combination of arguments."); } }?; let expected_version = @@ -71,9 +71,7 @@ fn run(arguments: Arguments) -> Result<(), git2::Error> { if let Some(current_version) = arguments.current_version { if current_version < expected_version { - let error_message = format!("The current version {current_version} is not larger or equal to the expected version {expected_version}."); - error!("{error_message}"); - return Err(git2::Error::from_str(&error_message)); + bail!(format!("The current version {current_version} is not larger or equal to the expected version {expected_version}.")); } info!("The current version {current_version} is larger or equal to the expected version {expected_version}."); } else {