Skip to content

Commit

Permalink
Merge pull request #2012 from hannobraun/error
Browse files Browse the repository at this point in the history
Improve error output
  • Loading branch information
hannobraun authored Sep 7, 2023
2 parents ee71c9c + 769402a commit 912f847
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions crates/fj/src/handle_model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{mem, ops::Deref};
use std::{error::Error as _, fmt, mem, ops::Deref};

use fj_core::{
algorithms::{
Expand Down Expand Up @@ -80,7 +80,7 @@ where
pub type Result = std::result::Result<(), Error>;

/// Error returned by [`handle_model`]
#[derive(Debug, thiserror::Error)]
#[derive(thiserror::Error)]
pub enum Error {
/// Error displaying model
#[error("Error displaying model")]
Expand All @@ -98,3 +98,31 @@ pub enum Error {
#[error(transparent)]
Validation(#[from] ValidationErrors),
}

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// When returning an error from Rust's `main` function, the runtime uses
// the error's `Debug` implementation to display it, not the `Display`
// one. This is unfortunate, and forces us to override `Debug` here.

// We should be able to replace this with `Report`, once it is stable:
// https://doc.rust-lang.org/std/error/struct.Report.html

write!(f, "{self}")?;

let mut source = self.source();

if source.is_some() {
write!(f, "\n\nCaused by:")?;
}

let mut i = 0;
while let Some(s) = source {
write!(f, "\n {i}: {s}")?;
source = s.source();
i += 1;
}

Ok(())
}
}

0 comments on commit 912f847

Please sign in to comment.