-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This was inspired by reading a blog post that showed some shortcomings of thiserror compared to snafu in capturing backtraces. The approach here captures a SpanTrace created by the tracing-error crate. https://docs.rs/tracing-error/ Note: this is a fairly mechanical copy of the relevant backtrace methods for discussion. I have not yet checked whether theres simplifications that make sense. It also is problematic due to the unstable version of the tracing crates, so it's doubtful that this should be merged as-is. My goal with this PR is to start a conversation about how it might work. Fixes #400
- Loading branch information
Showing
10 changed files
with
169 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"rust-analyzer.cargo.features": ["span_trace"] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use thiserror::Error; | ||
use tracing_error::SpanTrace; | ||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; | ||
|
||
fn main() { | ||
tracing_subscriber::registry() | ||
.with(tracing_subscriber::fmt::layer()) | ||
.with(tracing_error::ErrorLayer::default()) | ||
.init(); | ||
|
||
let result = boom(); | ||
match result { | ||
Err(err) => { | ||
eprintln!("error: {}", err); | ||
match err { | ||
Error::MyError(source, span_trace) => { | ||
eprintln!("source: {}", source); | ||
eprintln!("span trace: {:#?}", span_trace); | ||
} | ||
} | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
#[tracing::instrument] | ||
fn boom() -> Result<(), Error> { | ||
inner_boom()?; | ||
Ok(()) | ||
} | ||
|
||
#[tracing::instrument] | ||
fn inner_boom() -> Result<(), Error> { | ||
non_span_trace()?; | ||
Ok(()) | ||
} | ||
|
||
#[tracing::instrument] | ||
fn non_span_trace() -> std::io::Result<()> { | ||
std::fs::read_to_string("nonexistent-file")?; | ||
Ok(()) | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
enum Error { | ||
#[error("I/O error: {0}")] | ||
MyError(#[from] std::io::Error, SpanTrace), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters