This repository has been archived on 2026-03-27. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
tracing/examples/examples/map-traced-error.rs
Hayden Stainsby bdbaf80073
examples: add note to examples that they are for tracing 0.2.0 (#3099)
It is not uncommon that users who are new to tracing look at the
examples in the `master` branch of the repository and find that they
don't compile. This is because they are examples which compile with the
code from the master branch, which is for the as yet unreleased tracing
0.2.0 ecosystem.

Users should instead go to the `v0.1.x` branch to find examples
compatible with the crates published on crates.io.

This change adds a doc-comment to the beginning of every example file
informing the user of this fact and suggesting that they check out the
`v0.1.x` branch instead.
2024-10-09 15:31:11 -04:00

85 lines
2.2 KiB
Rust

//! NOTE: This is pre-release documentation for the upcoming tracing 0.2.0 ecosystem. For the
//! release examples, please see the `v0.1.x` branch instead.
//!
//! An example on composing errors inside of a `TracedError`, such that the
//! SpanTrace captured is captured when creating the inner error, but still wraps
//! the outer error.
#![deny(rust_2018_idioms)]
#![allow(clippy::try_err)]
use std::error::Error;
use std::fmt;
use tracing_error::{prelude::*, ErrorSubscriber, TracedError};
use tracing_subscriber::prelude::*;
fn main() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::subscriber())
// The `ErrorSubscriber` subscriber enables the use of `SpanTrace`.
.with(ErrorSubscriber::default())
.init();
let e = do_something().unwrap_err();
print_extracted_spantraces(&e);
}
// Iterate through the source errors and check if each error is actually the attached `SpanTrace`
// before printing it
fn print_extracted_spantraces(error: &(dyn Error + 'static)) {
let mut error = Some(error);
let mut ind = 0;
while let Some(err) = error {
if let Some(spantrace) = err.span_trace() {
eprintln!("Span Backtrace:\n{}", spantrace);
} else {
eprintln!("Error {}: {}", ind, err);
}
error = err.source();
ind += 1;
}
}
#[tracing::instrument]
fn do_something() -> Result<(), TracedError<OuterError>> {
do_the_real_stuff().map_err(TracedError::err_into)
}
#[tracing::instrument]
fn do_the_real_stuff() -> Result<(), TracedError<InnerError>> {
Err(TracedError::from(InnerError))
}
#[derive(Debug)]
struct OuterError {
source: InnerError,
}
impl Error for OuterError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.source)
}
}
impl fmt::Display for OuterError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "outer error message")
}
}
impl From<InnerError> for OuterError {
fn from(source: InnerError) -> Self {
Self { source }
}
}
#[derive(Debug)]
struct InnerError;
impl Error for InnerError {}
impl fmt::Display for InnerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "inner error message")
}
}