GithubHelp home page GithubHelp logo

Comments (3)

trickster avatar trickster commented on July 24, 2024 1

Thanks. I got it to work now with updated versions.

use color_eyre::{eyre::{self, WrapErr}};
use tracing::{error, info, instrument};
use tracing_error::{ErrorLayer, SpanTrace};
use tracing_subscriber::prelude::*;
use tracing_error::prelude::*;

#[derive(Debug)]
struct FooError {
    message: &'static str,
    context: SpanTrace,
}

impl FooError {
    fn new(message: &'static str) -> Self {
        Self {
            message,
            context: SpanTrace::capture(),
        }
    }
}

impl std::error::Error for FooError {}

impl std::fmt::Display for FooError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.pad(self.message)?;
        write!(f, "\n\nspan backtrace:\n{}", self.context)?;
        // write!(f, "\n\ndebug span backtrace: {:?}", self.context)?;
        // write!(f, "\n\nalt debug span backtrace: {:#?}", self.context)?;
        Ok(())
    }
}

#[instrument]
fn try_main(foo: &str) -> eyre::Result<(), eyre::Report> {
    info!("Doing something");
    do_another_thing(42, false)
}

#[instrument]
fn do_another_thing(answer: usize, will_succeed: bool) -> eyre::Result<(), eyre::Report> {
    info!("trying to do other thing");
    Err(FooError::new("something broke lol").in_current_span()).wrap_err_with(|| format!("this is additional message"))?;
    Ok(())
}

#[instrument]
fn main() {
    let json_fmt_layer = tracing_subscriber::fmt::Layer::new().json();
    tracing_subscriber::registry()
        .with(json_fmt_layer)
        .with(ErrorLayer::default())
        .init();

    if let Err(e) = try_main("hello world") {
        error!("error: {}", e);
        std::process::exit(1);
    }
}

With dependencies

[dependencies]
color-eyre = "0.5.11"
tracing = "0.1.29"
tracing-error = { version = "0.2.0", features = ["traced-error"] }
tracing-opentelemetry = "0.16.0"
tracing-subscriber = { version = "0.3.1", features = ["env-filter", "json", "fmt"] }

from color-eyre.

mdevlamynck avatar mdevlamynck commented on July 24, 2024

You probably need tracing-subscriber = "0.3.3" to use tracing-error = "0.2.0". Also since this crate depends on tracing-error = "0.1.2", span traces wont work anyway (all code must depend on the same version of tracing-error, color-eyre would depend on 0.1.2 and the example would depend on 0.2.0).

from color-eyre.

yaahc avatar yaahc commented on July 24, 2024

Yea, I'm going to release an update that brings all the versions back into alignment, for now you need to depend on older versions of tracing/tracing-subscriber to make it keep working

from color-eyre.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.