Skip to content

Commit bc7dec7

Browse files
authored
feat: expose Error::backtrace() (apache#1352) (#50)
1 parent 4f38d4a commit bc7dec7

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

crates/iceberg/src/error.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,49 @@ impl Error {
235235
self
236236
}
237237

238+
/// Return error's backtrace.
239+
///
240+
/// Note: the standard way of exposing backtrace is the unstable feature [`error_generic_member_access`](https://github.com/rust-lang/rust/issues/99301).
241+
/// We don't provide it as it requires nightly rust.
242+
///
243+
/// If you just want to print error with backtrace, use `Debug`, like `format!("{err:?}")`.
244+
///
245+
/// If you use nightly rust, and want to access `iceberg::Error`'s backtrace in the standard way, you can
246+
/// implement a newtype like this:
247+
///
248+
/// ```ignore
249+
/// // assume you already have `#![feature(error_generic_member_access)]` on the top of your crate
250+
///
251+
/// #[derive(::std::fmt::Debug)]
252+
/// pub struct IcebergError(iceberg::Error);
253+
///
254+
/// impl std::fmt::Display for IcebergError {
255+
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
256+
/// self.0.fmt(f)
257+
/// }
258+
/// }
259+
///
260+
/// impl std::error::Error for IcebergError {
261+
/// fn provide<'a>(&'a self, request: &mut std::error::Request<'a>) {
262+
/// request.provide_ref::<std::backtrace::Backtrace>(self.0.backtrace());
263+
/// }
264+
///
265+
/// fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
266+
/// self.0.source()
267+
/// }
268+
/// }
269+
/// ```
270+
///
271+
/// Additionally, you can add a clippy lint to prevent usage of the original `iceberg::Error` type.
272+
/// ```toml
273+
/// disallowed-types = [
274+
/// { path = "iceberg::Error", reason = "Please use `my_crate::IcebergError` instead." },
275+
/// ]
276+
/// ```
277+
pub fn backtrace(&self) -> &Backtrace {
278+
&self.backtrace
279+
}
280+
238281
/// Return error's kind.
239282
///
240283
/// Users can use this method to check error's kind and take actions.

0 commit comments

Comments
 (0)