Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more detail to error messages to enable their remediation #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ error_chain! {
display("decoding error: {}", msg)
}
#[doc="error in path canonicalization"]
CanonicalizationError {
CanonicalizationError(msg: String) {
description("canonicalization error")
display("canonicalization error: {}", msg)
}
#[doc="error in parsing the song header"]
HeaderParsingError {
HeaderParsingError(msg: String) {
description("header parsing error")
display("header parsing error: {}", msg)
}
#[doc="error in parsing the songs lines"]
LinesParsingError {
LinesParsingError(msg: String) {
description("lines parsing error")
display("lines parsing error: {}", msg)
}
}
}
Expand Down Expand Up @@ -72,7 +75,7 @@ fn canonicalize_path<B: AsRef<Path>>(
tmp_path.push(x);
let result = tmp_path
.canonicalize()
.chain_err(|| ErrorKind::CanonicalizationError)?;
.chain_err(|| ErrorKind::CanonicalizationError(format!("{:?}", tmp_path)))?;
Source::Local(result)
}
})
Expand All @@ -91,8 +94,22 @@ pub fn parse_txt_song<P: AsRef<Path>>(path: P) -> Result<TXTSong> {
let txt = read_file_to_string(path)?;

let mut txt_song = TXTSong {
header: parse_txt_header_str(txt.as_ref()).chain_err(|| ErrorKind::HeaderParsingError)?,
lines: parse_txt_lines_str(txt.as_ref()).chain_err(|| ErrorKind::LinesParsingError)?,
header: parse_txt_header_str(txt.as_ref())
.map_err(|e| {
let s = e.to_string();
Error::with_chain(
e,
ErrorKind::HeaderParsingError(s),
)
})?,
lines: parse_txt_lines_str(txt.as_ref())
.map_err(|e| {
let s = e.to_string();
Error::with_chain(
e,
ErrorKind::LinesParsingError(s)
)
})?,
};

// canonicalize paths
Expand Down
12 changes: 9 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ error_chain! {
display("additional {} tag found in line: {}", line, tag)
}
#[doc="an essential header is missing"]
MissingEssential {
MissingEssential(fields: Vec<String>) {
description("essential header is missing")
display("essential header is missing. Missing headers: {:?}", fields)
}

#[doc="value could not be parsed"]
Expand Down Expand Up @@ -232,7 +233,7 @@ pub fn parse_txt_header_str(txt_str: &str) -> Result<Header> {

// build header from Options
if let (Some(title), Some(artist), Some(bpm), Some(audio_path)) =
(opt_title, opt_artist, opt_bpm, opt_audio_path)
(opt_title.clone(), opt_artist.clone(), opt_bpm.clone(), opt_audio_path.clone())
{
let header = Header {
title,
Expand All @@ -255,8 +256,13 @@ pub fn parse_txt_header_str(txt_str: &str) -> Result<Header> {
// header complete
Ok(header)
} else {
let mut fields = Vec::new();
if opt_title.is_none() { fields.push("Title".to_string()) }
if opt_artist.is_none() { fields.push("Artist".to_string()) }
if opt_bpm.is_none() { fields.push("BPM".to_string()) }
if opt_audio_path.is_none() { fields.push("Audio Path".to_string()) }
// essential field is missing
bail!(ErrorKind::MissingEssential)
bail!(ErrorKind::MissingEssential(fields))
}
}

Expand Down