Comments (5)
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.
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.
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.
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.
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)
- Investigate switching `IntoError` from an associated type to generics HOT 2
- Investigate adding `ResultExt::boxed` and `::boxed_local`
- Is it possible to automatically include fields in error's display? HOT 2
- Consider adding #[snafu(transparent)] HOT 6
- Support Serde for the built-in types HOT 6
- multiple error types (struct and enum) and generic `IntoError` HOT 2
- Simple context usage, mysterious compile error HOT 2
- Use the same deprecation macros on the no_std Error as the official Error HOT 3
- Add an ensure macro that works with pattern matching HOT 2
- Restore support for yeeting context selectors HOT 1
- Print multi-line errors on their own lines in `Report` HOT 2
- Const generics seem to be giving the derive macro trouble HOT 2
- question: Best practice for automatic error conversion over two module levels?
- Cannot use a generic with a whatever variant
- Incorrect year in changelog
- No apparent way to have an error enum variant documentation generated from a display string HOT 5
- Why isn't Whatever Send + Sync by default? HOT 5
- `Report` print doesn't prefix with "Error: " HOT 1
- Is it possible to avoid manually capturing a backtrace when variant has no `source` error? HOT 2
- Document using `Option<Backtrace>` more prominently in docs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from snafu.