Skip to content

Commit

Permalink
[errors] add support for chained norad error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissimpkins committed Jan 28, 2022
1 parent 2f7d6eb commit 703646c
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/lib/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,29 @@ pub(crate) enum Error {
NoradWrite(PathBuf, norad::Error),
}

fn chained_error_fmt(
e: &impl std::error::Error,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
writeln!(f, "{}\n", e)?;
let mut current = e.source();
while let Some(cause) = current {
writeln!(f, "Caused by:\n\t{}", cause)?;
current = 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 Down

0 comments on commit 703646c

Please sign in to comment.