Skip to content

Commit

Permalink
Merge pull request #361 from palantir/wsuppiger/add-custom-trace
Browse files Browse the repository at this point in the history
Add custom backtrace for conjure error
  • Loading branch information
sfackler authored Jun 12, 2024
2 parents 1b877d9 + 12804f0 commit f22bbf1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-361.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Add custom backtrace for conjure error
links:
- https://github.com/palantir/conjure-rust/pull/361
27 changes: 23 additions & 4 deletions conjure-error/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ impl Error {
self
}

/// Adds a new custom backtrace to the error which is safe to log
#[inline]
pub fn with_custom_safe_backtrace(mut self, backtrace: String) -> Error {
self.0.backtraces.push(Backtrace::custom(backtrace));
self
}

/// Returns the error's backtraces, ordered from oldest to newest.
#[inline]
pub fn backtraces(&self) -> &[Backtrace] {
Expand Down Expand Up @@ -391,18 +398,30 @@ impl<'a> Iterator for ParamsIter<'a> {
}

/// A backtrace associated with an `Error`.
// FIXME remove in favor of std backtrace directly in next major bump
pub struct Backtrace(backtrace::Backtrace);
pub struct Backtrace(BacktraceInner);

impl fmt::Debug for Backtrace {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, fmt)
match &self.0 {
BacktraceInner::Rust(b) => fmt::Display::fmt(b, fmt),
BacktraceInner::Custom(b) => fmt::Display::fmt(b, fmt),
}
}
}

impl Backtrace {
#[inline]
fn new() -> Backtrace {
Backtrace(backtrace::Backtrace::force_capture())
Backtrace(BacktraceInner::Rust(backtrace::Backtrace::force_capture()))
}

#[inline]
fn custom(s: String) -> Backtrace {
Backtrace(BacktraceInner::Custom(s))
}
}

enum BacktraceInner {
Rust(backtrace::Backtrace),
Custom(String),
}

0 comments on commit f22bbf1

Please sign in to comment.