Skip to content

feat: expose Error::backtrace() #1352

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

Merged
merged 2 commits into from
May 19, 2025
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
43 changes: 43 additions & 0 deletions crates/iceberg/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,49 @@ impl Error {
self
}

/// Return error's backtrace.
///
/// Note: the standard way of exposing backtrace is the unstable feature [`error_generic_member_access`](https://github.com/rust-lang/rust/issues/99301).
/// We don't provide it as it requires nightly rust.
///
/// If you just want to print error with backtrace, use `Debug`, like `format!("{err:?}")`.
///
/// If you use nightly rust, and want to access `iceberg::Error`'s backtrace in the standard way, you can
/// implement a newtype like this:
///
/// ```ignore
/// // assume you already have `#![feature(error_generic_member_access)]` on the top of your crate
///
/// #[derive(::std::fmt::Debug)]
/// pub struct IcebergError(iceberg::Error);
///
/// impl std::fmt::Display for IcebergError {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// self.0.fmt(f)
/// }
/// }
///
/// impl std::error::Error for IcebergError {
/// fn provide<'a>(&'a self, request: &mut std::error::Request<'a>) {
/// request.provide_ref::<std::backtrace::Backtrace>(self.0.backtrace());
/// }
///
/// fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
/// self.0.source()
/// }
/// }
/// ```
///
/// Additionally, you can add a clippy lint to prevent usage of the original `iceberg::Error` type.
/// ```toml
/// disallowed-types = [
/// { path = "iceberg::Error", reason = "Please use `my_crate::IcebergError` instead." },
/// ]
/// ```
pub fn backtrace(&self) -> &Backtrace {
&self.backtrace
}

/// Return error's kind.
///
/// Users can use this method to check error's kind and take actions.
Expand Down
Loading