Skip to content

Commit

Permalink
Merge pull request #232 from chmp/feature/anyhow-display-rep
Browse files Browse the repository at this point in the history
Feature: anyhow display rep
  • Loading branch information
chmp authored Sep 16, 2024
2 parents bbe8a3e + 607d461 commit 23b8cb6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features
- Add support to serialize / deserialize `bool` from integer arrays
- Add a helper to construct `Bool8` arrays
- Include the path of the field that caused an error in the error message
- Include backtrace information only for the debug representations of errors

API changes

Expand Down
32 changes: 21 additions & 11 deletions serde_arrow/src/internal/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,18 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Common errors during `serde_arrow`'s usage
///
/// At the moment only a generic string error is supported, but it is planned to
/// offer concrete types to match against.
/// At the moment only a generic string error is supported, but it is planned to offer concrete
/// types to match against.
///
/// The error carries a backtrace if `RUST_BACKTRACE=1`, see [`std::backtrace`]
/// for details. This backtrace is included when printing the error. If the
/// error is caused by another error, that error can be retrieved with
/// [`source()`][std::error::Error::source].
/// The error carries a backtrace if `RUST_BACKTRACE=1`, see [`std::backtrace`] for details. This
/// backtrace is included when printing the error. If the error is caused by another error, that
/// error can be retrieved with [`source()`][std::error::Error::source].
///
/// # Display representation
///
/// This error type follows anyhow's display representation: when printed with display format (`{}`)
/// (or converted to string) the error does not include a backtrace. Use the debug format (`{:?}`)
/// to include the backtrace information.
///
#[derive(PartialEq)]
#[non_exhaustive]
Expand Down Expand Up @@ -150,18 +155,23 @@ impl std::cmp::PartialEq for CustomErrorImpl {

impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<{self}>")
write!(
f,
"Error: {msg}{annotations}\n{bt}",
msg = self.message(),
annotations = AnnotationsDisplay(self.annotations()),
bt = BacktraceDisplay(self.backtrace()),
)
}
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Error: {msg}{annotations}\n{bt}",
"Error: {msg}{annotations}",
msg = self.message(),
annotations = AnnotationsDisplay(self.annotations()),
bt = BacktraceDisplay(self.backtrace()),
)
}
}
Expand Down Expand Up @@ -194,8 +204,8 @@ impl<'a> std::fmt::Display for BacktraceDisplay<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0.status() {
BacktraceStatus::Captured => write!(f, "Backtrace:\n{bt}", bt=self.0),
BacktraceStatus::Disabled => write!(f, "No backtrace captured. Set the `RUST_BACKTRACE=1` env variable to enable."),
_ => write!(f, "No backtrace captured. Most likely backtraces are not supported on the current platform."),
BacktraceStatus::Disabled => write!(f, "Backtrace not captured; set the `RUST_BACKTRACE=1` env variable to enable"),
_ => write!(f, "Backtrace not captured: most likely backtraces are not supported on the current platform"),
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions serde_arrow/src/test/error_messages/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::internal::error::Error;

#[test]
fn backtrace_on_debug() {
let err = Error::custom(String::from("foo bar"));

// NOTE: the exact message depends on the ability of Rust to capture a backtrace
assert_eq!(format!("{}", err).contains("Backtrace"), false);
assert_eq!(format!("{:?}", err).contains("Backtrace"), true);
}
1 change: 1 addition & 0 deletions serde_arrow/src/test/error_messages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod deserializers;
mod misc;
mod push_validity;
mod trace_from_samples;
mod trace_from_type;

0 comments on commit 23b8cb6

Please sign in to comment.