Audit error types code base wide#2101
Merged
apoelstra merged 9 commits intorust-bitcoin:masterfrom Oct 6, 2023
Merged
Conversation
We typically layout error code as: definition, [impl block], `Display` impl, `error::Error` impl, from imlps. Code move only, no other changes.
Move the p2p error types to the bottom of the file next to the various impls for these types. Code move only, no other changes.
By convention we always include the suffix "Error" on our error types. Rename the error type `UnknownChainHash` to `UnknownChainHashError`.
By convention we always include the suffix "Error" on our error types. Rename the error type `UnknownMagic` to `UnknownMagicError`.
By convention we always include the suffix "Error" on our error types. Rename the error type `IncompleteBuilder` to `IncompleteBuilderError`.
By convention we always include the suffix "Error" on our error types. Rename the error type `HiddenNodes` to `HiddenNodesError`.
We would like the codebase to be optimized for readability not ease of development, as such code that is write-once-read-many should not use macros. Currently we use the `impl_std_error` macro to implement `std::error::Error` for struct error types. This makes the code harder to read at a glance because one has to think what the macro does. Remove the `impl_std_error` macro and write the code explicitly.
In a further effort to make the code brain-dead easy to read; use an explicit implementation of `std::error::Error` that returns `None` instead of relying on the default trait implementation.
On our way to v1.0.0 we are defining a standard for our error types, this includes: - Uses the following derives (unless not possible, usually because of `io::Error`) `#[derive(Debug, Clone, PartialEq, Eq)]` - Has `non_exhaustive` unless we really know we can commit to not adding anything. Furthermore, we are trying to make the codebase easy to read. Error code is write-once-read-many (well it should be) so if we make all the error code super uniform the users can flick to an error and quickly see what it includes. In an effort to achieve this I have made up a style and over recent times have change much of the error code to that new style, this PR audits _all_ error types in the code base and enforces the style, specifically: - Is layed out: definition, [impl block], Display impl, error::Error impl, From impls - `error::Error` impl matches on enum even if it returns `None` for all variants - Display/Error impls import enum variants locally - match uses *self and `ref e` - error::Error variants that return `Some` come first, `None` after Re: non_exhaustive To make dev and review easier I have added `non_exhaustive` to _every_ error type. We can then remove it error by error as we see fit. This is because it takes a bit of thinking to do and review where as this patch should not take much brain power to review.
f68b484 to
10374af
Compare
apoelstra
added a commit
to rust-bitcoin/rust-bech32
that referenced
this pull request
Oct 4, 2023
9b23c9c Audit error types (Tobin C. Harding) Pull request description: Audit all error types and ensure the following holds: - All use `non_exhaustive` - All derive `Debug, Clone, PartialEq, Eq` (unless `io::Error` is present, in which case only `Debug`) - All error `From` impls use `inline` This is similar to rust-bitcoin/rust-bitcoin#2101, probably should have done this one first. ACKs for top commit: apoelstra: ACK 9b23c9c Tree-SHA512: af525afc8d1a95327e40f7983afe905be18cfe9040b11ca4c14ec93329e32a52a830937c2d79b6b02c763dc17c9a7490871779094ab0fded64654852550a94c9
Member
|
This all looks great to me! |
apoelstra
approved these changes
Oct 4, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR aims to achieve two things:
The first 8 patches are pretty basic, and are broken up into really small changes. The last patch is much bigger, it has a long git log to explain it but reviewing should not take too much brain power.
This PR does not introduce anything new, it just applies what we have been doing recently with errors. Before v1.0.0 others will likely want to re go over all the error types. As such I believe this PR can be merged under the one ack carve-out.
TODOs (future PRs)
We have a few errors that still need splitting up:
merkle_tree::block::MerkleBlockErrorpsbt::error::ErrorIncompleteBuilderErrorAlso, all error From's should probably have
#[inline], I noticed late in the process and did not have the heart to visit every error again.