Skip to content

Implement chained error reporting support #44

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

Open
wants to merge 5 commits into
base: main
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
25 changes: 22 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ exclude = [
]

[dependencies]
norad = { version = "0.6.0", features = ["rayon"] }
# norad = { version = "0.6.0", features = ["rayon"] }
norad = { git = "https://github.com/linebender/norad", branch = "master" }
structopt = "0.3"
colored = "2.0"
rayon = "1.5"
Expand Down
28 changes: 22 additions & 6 deletions src/lib/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,34 @@ lazy_static! {
#[derive(Debug)]
pub(crate) enum Error {
InvalidPath(PathBuf),
NoradRead(PathBuf, norad::Error),
NoradWrite(PathBuf, norad::Error),
NoradRead(PathBuf, norad::error::FontLoadError),
NoradWrite(PathBuf, norad::error::FontWriteError),
}

// Implementation adapted from https://www.lpalmieri.com/posts/error-handling-rust/
fn chained_error_fmt(
e: &impl std::error::Error,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
writeln!(f, "{}\n", e)?;
let mut current_err = e.source();
while let Some(err_cause) = current_err {
writeln!(f, "Caused by:\n\t{}", err_cause)?;
current_err = err_cause.source();
}
Ok(())
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
match &self {
Error::NoradRead(p, e) => {
write!(f, "norad read error: {}: {}", p.display(), e)
writeln!(f, "norad read error: {}", p.display())?;
chained_error_fmt(e, f)
}
Error::NoradWrite(p, e) => {
write!(f, "norad write error: {}: {}", p.display(), e)
writeln!(f, "norad write error: {}", p.display())?;
chained_error_fmt(e, f)
}
Error::InvalidPath(p) => {
write!(f, "invalid path error: {} was not found", p.display())
Expand All @@ -49,14 +65,14 @@ mod tests {

#[test]
fn test_ufofmterror_read() {
let ne = norad::Error::MissingLayer("test".to_owned());
let ne = norad::error::FontLoadError::MissingDefaultLayer;
let ufe = Error::NoradRead(PathBuf::from("test.ufo"), ne);
assert!(ufe.to_string().starts_with("norad read error: "));
}

#[test]
fn test_ufofmterror_write() {
let ne = norad::Error::MissingLayer("test".to_owned());
let ne = norad::error::FontWriteError::PreexistingPublicObjectLibsKey;
let ufe = Error::NoradWrite(PathBuf::from("test.ufo"), ne);
assert!(ufe.to_string().starts_with("norad write error: "));
}
Expand Down