-
Notifications
You must be signed in to change notification settings - Fork 9
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
Hide error internals #129
Hide error internals #129
Conversation
As we do for the other error types add getters and make the fields private.
In preparation for hiding error internals; use the already existing error getter functions. Internal change only.
The default implementation for `std::error::Error` returns `None`. Adding a custom impl that does the same adds no value - remove it. Internal change only, no logic change.
Instead of matching on `Err(Foo)` just call `unwrap_err` and match on `Foo`. Done in preparation for hiding errors so we can use `.0` to get at the inner error.
7d9ee4b
to
2df135a
Compare
In c35eba5: I think, rather than having a pair of accessors that return options, we should just expose As written, we have a ton of API freedom -- as you say, we can do whatever we want to the error type and maybe eventually make both accessors just return However, if in rust-bitcoin we're already checking with |
You might say -- why even add the The reason is to let us add extra context or other fields to the main error type. We could also make the |
I had a bit of a play, wrote a long response, learned something, and deleted it - WIN. Take 2 ... You mean this, right? /// Hex decoding error.
#[derive(Debug, Clone, PartialEq, Eq)]
// We are free to change the inner type.
pub struct HexToBytesError(pub(crate) CharLengthError);
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CharLengthError { // Name describing exactly these two variants.
/// Non-hexadecimal character.
InvalidChar(InvalidCharError),
/// Purported hex string had odd length.
OddLengthString(OddLengthStringError),
} And have a getter that returns If I got you correctly then I think a few things:
Reasons:
|
I wouldn't even have it return an
Lol, and then this just does it again for no reason. If we're already returning an
Yes, but if users want to match on errors then this is what we need to expose
It does provide value -- as evidenced by it being used in rust-bitcoin. If our API forces us to add unwraps in rust-bitcoin then it's not a reasonable API. |
I thought that the point of hiding the errors was explicitly to prevent this? In no other code, where we have hidden errors, are we left with a public enum (IIRC). |
No, definitely not. The point is that allowing this ties our hands and (in most cases) nobody is doing it so there's no reason to do so. But in this case people clearly are using it. So then we're removing actual, necessary functionality for the sake of giving ourselves more API freedom. |
When I see stuff like rust-bitcoin/rust-bitcoin#3835 it's tempting to just 1.0 the existing hex errors without closing them at all. But I think we can do a bit of closing -- in particular it would be good to replace the "top-level errors" which are directly returned with opaque structs. Even if we have accessors that basically force the internal representation to stay the same, this gives us freedom to add additional context to them if we want to. |
Cool, thanks. I'll have another play with it. |
Structs with private fields do not need `non_exhaustive`. Remove unnecessary `non_exhaustive` attribute from `InvalidLengthError`.
We would like to give ourselves maximum forward flexibility while not rugging users who currently use inners of our top level errors (`HexToBytesError` and `HexToArrayError`). Add two new public error enums that match the current top level errors. Use these new errors inside the current ones and add a getter to access it.
c35eba5
to
2b88813
Compare
Check it out, this is pretty nice if I don't say so myself. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 2b88813; successfully ran local tests; looks great!
In an effort to make give us flexibility in the
1.0
release hide all the error internals.InvalidLengthError
non_exhaustive
rust-bitcoin
(tested in: DEMO: Use hex with hidden errors rust-bitcoin#3830)