Skip to content

Commit 0386134

Browse files
authored
feat: expose Error::backtrace() (#1352)
Signed-off-by: xxchan <xxchan22f@gmail.com> ## Which issue does this PR close? #1340 ## What changes are included in this PR? ## Are these changes tested? --------- Signed-off-by: xxchan <xxchan22f@gmail.com>
1 parent cfe04b2 commit 0386134

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
@@ -256,6 +256,49 @@ impl Error {
256256
self
257257
}
258258

259+
/// Return error's backtrace.
260+
///
261+
/// Note: the standard way of exposing backtrace is the unstable feature [`error_generic_member_access`](https://github.com/rust-lang/rust/issues/99301).
262+
/// We don't provide it as it requires nightly rust.
263+
///
264+
/// If you just want to print error with backtrace, use `Debug`, like `format!("{err:?}")`.
265+
///
266+
/// If you use nightly rust, and want to access `iceberg::Error`'s backtrace in the standard way, you can
267+
/// implement a newtype like this:
268+
///
269+
/// ```ignore
270+
/// // assume you already have `#![feature(error_generic_member_access)]` on the top of your crate
271+
///
272+
/// #[derive(::std::fmt::Debug)]
273+
/// pub struct IcebergError(iceberg::Error);
274+
///
275+
/// impl std::fmt::Display for IcebergError {
276+
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
277+
/// self.0.fmt(f)
278+
/// }
279+
/// }
280+
///
281+
/// impl std::error::Error for IcebergError {
282+
/// fn provide<'a>(&'a self, request: &mut std::error::Request<'a>) {
283+
/// request.provide_ref::<std::backtrace::Backtrace>(self.0.backtrace());
284+
/// }
285+
///
286+
/// fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
287+
/// self.0.source()
288+
/// }
289+
/// }
290+
/// ```
291+
///
292+
/// Additionally, you can add a clippy lint to prevent usage of the original `iceberg::Error` type.
293+
/// ```toml
294+
/// disallowed-types = [
295+
/// { path = "iceberg::Error", reason = "Please use `my_crate::IcebergError` instead." },
296+
/// ]
297+
/// ```
298+
pub fn backtrace(&self) -> &Backtrace {
299+
&self.backtrace
300+
}
301+
259302
/// Return error's kind.
260303
///
261304
/// Users can use this method to check error's kind and take actions.

0 commit comments

Comments
 (0)