Skip to content

Commit

Permalink
feat: disable backtrace for NotFound error (#5577)
Browse files Browse the repository at this point in the history
#5569

Signed-off-by: xxchan <[email protected]>
  • Loading branch information
xxchan authored Jan 27, 2025
1 parent cdbcc0f commit c811a30
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion core/src/types/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ impl ErrorKind {
pub fn into_static(self) -> &'static str {
self.into()
}

/// Capturing a backtrace can be a quite expensive runtime operation.
/// For some kinds of errors, backtrace is not useful and we can skip it (e.g., check if a file exists).
///
/// See <https://github.com/apache/opendal/discussions/5569>
fn disable_backtrace(&self) -> bool {
matches!(self, ErrorKind::NotFound)
}
}

impl Display for ErrorKind {
Expand Down Expand Up @@ -314,7 +322,11 @@ impl Error {
source: None,
// `Backtrace::capture()` will check if backtrace has been enabled
// internally. It's zero cost if backtrace is disabled.
backtrace: Backtrace::capture(),
backtrace: if kind.disable_backtrace() {
Backtrace::disabled()
} else {
Backtrace::capture()
},
}
}

Expand Down

0 comments on commit c811a30

Please sign in to comment.