-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No apparent way to have an error enum variant documentation generated from a display string #445
Comments
That would be ill advised as per the recommendations from the error handling WG. If an error contains an exposed source error (the default when the field is named The following example would follow this recommendation by using an error reporter and letting it traverse the chain to eventually expand the message implemented by #[derive(Debug, Snafu)]
pub enum FooBarError {
/// The foo-bar will not frobinate properly given the alignment of splines
FooBarFrobinationFailure { source: SomeSourceError },
} On the second example, The following is succinct and should give you a sufficiently clear message. #[derive(Debug, Snafu)]
pub enum FooBarError {
/// The foo-bar will not frobinate properly for spline alignment {spline_alignment}
FooBarFrobinationFailure { spline_alignment: SplineAlignment },
} A sub-macro to concatenate the doc-comment with an additional templated string could probably exist (though I'll let @shepmaster chime in because macros are not my forte), but one may question the added value of introducing this construct. We would be increasing the complexity of the macro to save a few words, right? |
That's interesting. I wasn't familiar with those recommendations for the error handling WG. So presumably
Does that work? I.e will Snafu translate that into a correct error string? The generated docs show the format string as {spline_alignment} (as one would expect).
You're not really wrong - I've been doing these errors for years without feeling the need to raise an issue, so it's clearly marginal, but I finally got fed up with repeating myself today! |
Yes, and you can select another field when it is not named
You can use the report API in snafu itself. Other error handling libraries such as
I proposed that myself. :) |
Ok, I think this is sufficient workaround for my irritation. It will be interesting to hear if @shepmaster has an opinion, but I think I'm broadly happy. Great work on Snafu in any case (a happy user since 2019)! |
As my initial thought, I'm hesitant to add something like "combine the docstring with some more text to generate the For example, maybe my docstring is "hello world" and I want the error to be "welcome to the hello world error" — now I want to prepend and append. Or maybe I need to change some casing or punctuation in the middle anyway. That being said, having the |
If one wishes to have the enum variant documented in the library documentation (i.e. from
cargo doc
) as well as having nice run-time error messages, my understanding from the documentation and playing around a bit is that one must have two separate lines - a doc line and asnafu(display())
line. I find myself repeatedly doing this and having to replicate the error string twice with minor perturbations which is a bit irritating.So this is what I often write most commonly:
Or a slightly less common, but still pretty common:
Both of these result in a load of extra writing to get both the doc string and the error string.
I would be nice if the two could be consolidated somewhat and boiler plate reduced. Something like the following might be nice:
which implicitly appends the source to the doc string error to create the display
And a more general version:
If I missed something obvious, please do let me know!
The text was updated successfully, but these errors were encountered: