From 6cb758b9fd89935e3184fab1ccdf1169afd71c3c Mon Sep 17 00:00:00 2001 From: colomboalb Date: Wed, 15 Jan 2020 09:22:42 +0100 Subject: [PATCH 1/2] feat(issue-444): make error reporting more user friendly Intercept error `Result`s and log them properly, in red, printing only the description. This looks a lot better, to a final user, than the json dump we used to have. --- src/main.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0e5d6ab..ef84f39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use fallible_iterator::FallibleIterator; use semver::Version; use simple_error::SimpleError; use structopt::StructOpt; -use log::warn; +use log::{warn, error}; use crate::bumping::{Bump, BumpType}; use crate::changelog::ChangeLog; @@ -78,9 +78,25 @@ impl bumping::ObserveParseError for Vec { } } -fn main() -> Result<(), Box> { +fn main() { let config: Config = Config::from_args(); - stderrlog::new().module(module_path!()).verbosity(config.verbose + 2).init().unwrap(); + + if let Err(e) = what_bump(config) { + error!("{}", e); + print_error_cause(e.as_ref()); + std::process::exit(1); + } +} + +fn print_error_cause(error: &dyn Error) { + if let Some(error) = error.source() { + error!("caused by {}", error); + print_error_cause(error); + } +} + +fn what_bump(config: Config) -> Result<(), Box> { + stderrlog::new().module(module_path!()).verbosity(config.verbose + 2).init()?; match (config.bump, &config.from) { (Some(ref bump_type), Some(ref version)) => { From f28bc9c6e19e21d7d0911deadf1cfbb4cb88d940 Mon Sep 17 00:00:00 2001 From: colomboalb Date: Wed, 15 Jan 2020 09:31:52 +0100 Subject: [PATCH 2/2] add debug logging to the `BumpType` parser --- src/bumping.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/bumping.rs b/src/bumping.rs index 8c008d5..abd490e 100644 --- a/src/bumping.rs +++ b/src/bumping.rs @@ -6,6 +6,7 @@ use std::str::FromStr; use git2::Commit; use semver::Version; use simple_error::SimpleError; +use log::debug; /// Extension methods for `&str`, useful for handling conventional commits pub trait FirstLine<'a> { @@ -80,7 +81,7 @@ impl<'a> TryFrom> for LogEntry<'a> { /// The different types of version bumps that one can do /// /// Can be created from a commit message. -#[derive(Debug, Eq, Ord, PartialOrd, PartialEq)] +#[derive(Copy, Clone, Debug, Eq, Ord, PartialOrd, PartialEq)] pub enum BumpType { None, Patch, @@ -147,19 +148,21 @@ impl BumpType { let first_line = commit_msg.first_line(); let conventional_prefix = first_line.prefix(); let breaking = conventional_prefix.contains('!') || commit_msg.contains("\nBREAKING CHANGE"); - - if breaking { - BumpType::Major - } else if conventional_prefix.starts_with("fix") { - BumpType::Patch - } else if conventional_prefix.starts_with("feat") { - BumpType::Minor - } else { - if !OTHER_TYPES.contains(&conventional_prefix.as_str()) { - error_observer.on_error(commit_msg, first_line); - } - BumpType::None - } + let result = + if breaking { + BumpType::Major + } else if conventional_prefix.starts_with("fix") { + BumpType::Patch + } else if conventional_prefix.starts_with("feat") { + BumpType::Minor + } else { + if !OTHER_TYPES.contains(&conventional_prefix.as_str()) { + error_observer.on_error(commit_msg, first_line); + } + BumpType::None + }; + debug!(r#"parsed "{}" into {} bump"#, first_line, result); + result } }