Skip to content

Commit f9cccb6

Browse files
committed
[errors] add support for chained norad error reporting
#43
1 parent 4fa0b39 commit f9cccb6

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/lib/errors.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,29 @@ pub(crate) enum Error {
1919
NoradWrite(PathBuf, norad::Error),
2020
}
2121

22+
fn chained_error_fmt(
23+
e: &impl std::error::Error,
24+
f: &mut std::fmt::Formatter<'_>,
25+
) -> std::fmt::Result {
26+
writeln!(f, "{}\n", e)?;
27+
let mut current = e.source();
28+
while let Some(cause) = current {
29+
writeln!(f, "Caused by:\n\t{}", cause)?;
30+
current = cause.source();
31+
}
32+
Ok(())
33+
}
34+
2235
impl fmt::Display for Error {
2336
fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
2437
match &self {
2538
Error::NoradRead(p, e) => {
26-
write!(f, "norad read error: {}: {}", p.display(), e)
39+
writeln!(f, "norad read error: {}", p.display())?;
40+
chained_error_fmt(e, f)
2741
}
2842
Error::NoradWrite(p, e) => {
29-
write!(f, "norad write error: {}: {}", p.display(), e)
43+
writeln!(f, "norad write error: {}", p.display())?;
44+
chained_error_fmt(e, f)
3045
}
3146
Error::InvalidPath(p) => {
3247
write!(f, "invalid path error: {} was not found", p.display())

0 commit comments

Comments
 (0)