Comments (6)
I'd like to take a shot at this since it bit me. :)
I've got it reporting a better error that points to the enum variant with the source
attribute. (I couldn't think of a better span
for the error message.) Does this seem OK? Is this the right error type?
- SnafuAttribute::Source(..) => { /* Report this isn't valid here? */ }
+ SnafuAttribute::Source(..) => {
+ return Err(vec![syn::Error::new(
+ name.span(),
+ "'source' attribute is only valid on individual fields; attempted to place on this variant:",
+ )])
+ }
(name
is set to variant.ident
a few lines above.)
Here's the result, with the bad code from https://gist.github.com/tjkirch/8917dd863bc284db20384fb303651ea6:
error: 'source' attribute is only valid on individual fields; attempted to place on this variant:
--> src/main.rs:10:5
|
10 | Recursive { source: Box<Error> },
| ^^^^^^^^^
If this seems OK, I can do something similar for the other "Report this isn't valid here" comments.
from snafu.
I'd like to take a shot at this since it bit me. :)
Wonderful, thank you!
a better
span
for the error message
That’s a tough question. In an ideal world, we’d have the ability to add help
text, and maybe that would point to the source
field if present and say “did you mean to add it here”, but (a) I don’t think there is that ability (b) it might not be where they wanted it anyway.
The other thing I could think of would be to point at the attribute itself, so that it was very clear which attribute we are talking about. This would probably involve keeping some span information around from attribute parse time.
That being said, I think that any error will save people some heartache, so I’m down with getting messages in and iterating.
I can do something similar for the other "Report this isn't valid here" comments.
One thing to note is that I’d like to try and report as many errors in one pass as possible. For example, this should generate two errors, one for source
and one for backtrace
:
#[derive(Snafu)]
enum X {
#[snafu(source)]
#[snafu(backtrace)]
Variant,
}
I’m still figuring out great patterns for this kind of work. In another branch, I have a macro that allows multiple back-to-back errors to happen.
In this case, we might want a vector to collect all the errors in and then return it; something like:
let mut errors = Vec::new();
for attr in attributes_from_syn(variant.attrs)? { … }
if !errors.is_empty() { return Err(errors) }
from snafu.
Feel free to open a draft PR early. Unfortunately for you, I’m fairly nit-picky in my reviews 😇
from snafu.
let mut errors = Vec::new();
for attr in attributes_from_syn(variant.attrs)? { … }
if !errors.is_empty() { return Err(errors) }
First, I tried a Vec for the entire variants.map(|variant|
so we could report errors for all attributes of all variants at once instead of just the attributes of the first variant with an error. That seemed to work, and seems more complete, so I'll keep going with that unless you say it's bad for some reason :)
from snafu.
for the entire
variants.map(|variant|
Yep, that was my eventual idea, but I wasn’t sure how complicated it would end up being, so I didn’t want to bring it up. For example, that Vec
should be able to mesh with other not-immediately-fatal errors, like the thing mentioned in my WIP branch.
from snafu.
Yep, that was my eventual idea, but I wasn’t sure how complicated it would end up being, so I didn’t want to bring it up. For example, that
Vec
should be able to mesh with other not-immediately-fatal errors, like the thing mentioned in my WIP branch.
It was trivial to do the larger scope in the end. I didn't look through all of the existing errors, but I did see some that might fit better into the Vec than as an immediate return. I left that for separate consideration, though.
from snafu.
Related Issues (20)
- alternate `Display` to include the source chain HOT 4
- Best way to consolidate error types HOT 9
- 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
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.