GithubHelp home page GithubHelp logo

Comments (5)

CryZe avatar CryZe commented on July 27, 2024 2

I actually just implemented the Debug derive and the chain iterators by coincidence too. I'll make a PR for these later :D

Oh, looks like you even went ahead and made a Debug impl that prints the backtrace and the chain within it. With my implementation you would use the sources iterator on the outside and then print the individual errors inside the loop. Maybe snafu could have a top level error wrapper type that has a Debug and Display impl that just iterates the wrapped error and then visualizes it nicely.

Maybe something like this:

fn main() -> Result<(), snafu::ShowError> {
    my_inner_fn().context(Custom { backtrace: true, sources: true })?;
    Ok(())
}

Although maybe the customization via context is overkill, not sure.

from snafu.

killercup avatar killercup commented on July 27, 2024 1

Oh, great timing! I just made https://github.com/killercup/snafu-cli-debug because I wrote that code several times now. It's very much an 0.1, and could definitely use more tweaking. Also feel free to just adopt the code into the snafu derive.

from snafu.

shepmaster avatar shepmaster commented on July 27, 2024 1

I wrote that code several times now

Neat, thanks for sharing!

A quick skim shows that it has some additional features I hadn’t thought about:

  • Printing a causal chain
  • Printing the backtrace

These make sense, but may also want to be possible to opt-out. Something to consider when figuring out which syntax to use to enable this.

We may also want to steal the “causal chain iterator” idea from (I forget which library… error-chain?) so that we could just write something like:

for source in SourceIter::new(self) {
    ::std::writeln!(f, "\tcause: {}", source)?;
}

I think that would make sense as a little tool to provide for people to use and allow us to backport iter_sources if it ever stabilizes.

from snafu.

sbromling avatar sbromling commented on July 27, 2024 1

We have tried out snafu-cli-debug, and ended up writing our own causal-chain printer for log messages. One side effect of this is that we no longer want the default Display implementation of a Snafu Error to explicitly print the source(). That's easy enough to accomplish with either the #[snafu(display(...))] attribute or a documentation comment (///), but we've discovered that as of #58, "bare" variants automatically print the source. Easy enough to avoid, but it would be nice to have that behavior configurable :-)

from snafu.

shepmaster avatar shepmaster commented on July 27, 2024

An interesting idea! This could look something like:

#[derive(Snafu, SnafuDebug)]
enum Error { /* … */ }

// Or maybe
#[derive(Snafu)]
#[snafu(Debug)]
enum Error { /* … */ }

// Or maybe
#[derive(Snafu(Debug))] // Is this even possible?
enum Error { /* … */ }

Ideally, switching between built-in Debug and SNAFU-powered Debug should be trivial for when developing and you need to debug.

The generated implementation should be trivial:

// Include initial generics and bounds…
// May need `Self: Display`
impl Debug for Error {
    fn fmt(&self, f) -> R {
        Display::fmt(self, f)
    }
}

One thought while typing the examples is that it feels like its about debugging SNAFU itself, not implementing Debug. Not sure how to work around that.

from snafu.

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.