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
Provide access to the original error for ValueValidation
errors
#2151
Comments
For me, the solution is to change the Display implementation or your OuterError to what you want. |
That would be against the modern error handling standards where an error should only print its own details and rely on an error reporting implementation (such as EDIT: And be against long-time error guidelines such as C-GOOD-ERR which says
|
Then, returns a anyhow::Result in FronStr, and if that's still not good, use a custom validator/parser that returns the kind of error you want. I don't think that's clap work to do complicated error formating. |
I do return an
That's precisely why I believe |
Quickly looking into what would be necessary to implement this, there would need to be at least one breaking change of changing |
I tagged this as 3.0 in case we want to decide on changing the API before release. |
If you want I can prepare a PR with this change to look at, I just quickly hacked it in to test the behaviour and it works well with both |
Sounds good |
@Nemo157 Any update with the PR? |
Sorry, I prepared the change and got it passing tests, but I don't remember if there was anything else I needed to check before opening a PR, I'll try to get it open tonight. |
Describe your use case
I have a
FromStr
implementation that returns a chained error providing more context when decoding an argument fails. When this error is returnedclap
(using v3'sderive
andClap::try_parse
) serializes just the outer context into a string and attaches that to theValueValidation
error returned, removing the more important information contained inside the inner error.Here's a (not that small) minimal representation of the issue:
Running this with
cargo run -- test
we can see that the returned error only contains:losing the
InnerError
completely.Describe the solution you'd like
Add an
Option<Box<dyn Error>>
onto theError
struct and support theError::source
method to get access to this error.This would cause (additional) issues with the current
Display
implementation, when using an error reporter such asanyhow::Error
which chains the causes on; you would get duplicate error messages because ofimpl Display for clap::Error
printing thecause
already. (Additional because you already get duplicateError:
prefixes fromclap::Error
printing this prefix itself).The text was updated successfully, but these errors were encountered: