Skip to content

Commit

Permalink
feat(issue-4): make error reporting more user friendly (#11)
Browse files Browse the repository at this point in the history
* 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.

* add debug logging to the `BumpType` parser
  • Loading branch information
albx79 authored Jan 15, 2020
1 parent e156277 commit eb46d1d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
31 changes: 17 additions & 14 deletions src/bumping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -80,7 +81,7 @@ impl<'a> TryFrom<Commit<'a>> 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,
Expand Down Expand Up @@ -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
}
}

Expand Down
22 changes: 19 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,9 +78,25 @@ impl bumping::ObserveParseError for Vec<ParseError> {
}
}

fn main() -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
stderrlog::new().module(module_path!()).verbosity(config.verbose + 2).init()?;

match (config.bump, &config.from) {
(Some(ref bump_type), Some(ref version)) => {
Expand Down

0 comments on commit eb46d1d

Please sign in to comment.