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

Improve error output #2012

Merged
merged 1 commit into from
Sep 7, 2023
Merged
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
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(())
}
}