shepmaster / snafu

Easily assign underlying errors into domain-specific errors while adding context
https://docs.rs/snafu/
Apache License 2.0
1.45k stars 61 forks source link

Add a troubleshooting section to the guide with common errors #212

Closed shepmaster closed 3 years ago

shepmaster commented 4 years ago

A common error is attempting to use MyErrorEnum::Variant when the context selector Variant should be used instead. It's worth documenting this.

Are there any other common issues?

0xpr03 commented 4 years ago

Another problem I've got, from which this originated: I was always using something like Err(MyError::ResponseLimit { ... }). That works well untill you try to emit a leaf error including a backtrace. Then you need ResponseLimit { ... }. But you can't do return Err(ResponseLimit { ... });, because that's not a MyError, it also won't work with return Err(MyError::ResponseLimit(ResponseLimit { ... })). The fix is to use return ResponseLimit { ... }.fail(); and it works magically. I found that by accident browsing through the docs again.

redragonx commented 4 years ago

Another problem I found while learning SNAFU is that https://github.com/shepmaster/snafu/blob/2c2e6ea5617be02c06e0a8144a5e391de8cd067e/src/guide/attributes.md#controlling-visibility was nowhere to be found in the main docs.

Everything clicked after I found this guide.

shepmaster commented 4 years ago

Using .into() inside a context selector causes

cannot infer type for type parameter `__T0` declared on the struct ...

e.g.

Foo { message: "hello".into() }.fail()