-
Notifications
You must be signed in to change notification settings - Fork 827
Add custom error for invalid parsing of address types #1091
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
Add custom error for invalid parsing of address types #1091
Conversation
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.
Thanks for your contribution! High level it looks nice, I suggested a few minor improvements.
Err(Error::InvalidAddressType("invalid".to_string())) | ||
); | ||
} | ||
|
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.
Admittedly our test practices are a bit loosey-goosey but this one could be made super clean by doing
#[test]
fn valid_address_parses_correctly() {
let addr = AddressType::from_str("p2tr").expect("false negative while parsing address");
assert_eq!(addr, AddressType::P2tr);
}
#[test]
fn invalid_address_parse_error() {
let got = AddressType::from_str("invalid");
let want = Err(Error::InvalidAddressType("invalid".to_string()));
assert_eq!(got, want);
}
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.
Bad github-foo, I did not add enough context when doing this suggestion, my bad.
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.
Got you! Yeah, this is done too.
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.
I was actually thinking about iterating over all of the valid address types in the test, but didn't do it in the end because I thought it to be slightly over-kill. Wdyt? I've left it as you suggested, but happy to add that if it seems worth it! :)
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.
Tempting but not sure if worth it either.
@tcharding - cheers for the review! That was quick! I've made the changes you requested 👍 It should look better now! |
Sweet, thanks mate. Can you squash it all down into a single commit and force push please. In general if you make changes to a PR after review that effect a previous commit in the PR then you can just squash it. Unless there is some extra reason not to. |
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.
Looks good. Some bike shedding: maybe UnknownAddressType
is better because there could be a new one in the future and it wouldn't be invalid, just not known to this library.
Err(Error::InvalidAddressType("invalid".to_string())) | ||
); | ||
} | ||
|
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.
Tempting but not sure if worth it either.
Good point @Kixunil! Perhaps the display for the error should also be changed from: What do you think about this? |
@tcharding - Of course! Will do, after addressing @Kixunil's change requests. Thanks for the tip 👍 |
Adds a custom error `UnknownAddressType(_)` which is returned in cases where an unknown address type tries to be parsed via `FromStr`. This provides more context for the error. For more info see [1]. [1] - `https://github.com/rust-bitcoin/rust-bitcoin/issues/1064`
0f85476
to
19ba7ec
Compare
Force-pushed to 19ba7ec. This includes the changes requested by @tcharding; namely:
Also, added some new changes requested by @Kixunil:
|
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.
Perfect!
ACK 19ba7ec
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 19ba7ec
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 19ba7ec
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 19ba7ec
…rsing of address types 19ba7ec Add custom error for unknown address type parsing (Arturo Marquez) Pull request description: Adds a custom error `UnknownAddressType(_)` which is returned in cases where an unknown address type tries to be parsed via `FromStr`. This provides more context for the error, since it contains the `String` that tried to be parsed. Closes rust-bitcoin/rust-bitcoin#1064 ACKs for top commit: Kixunil: ACK 19ba7ec dunxen: ACK 19ba7ec tcharding: ACK 19ba7ec apoelstra: ACK 19ba7ec Tree-SHA512: 2f94eb2e122c1acafcb8c852f7bfe22cb3725806541ae40f82d3a882011fb911ce8fc430153ebea4066f43c5a51813359a4c9b95d2a9077ce8f6dcaa58eee6fd
Adds a custom error
UnknownAddressType(_)
which is returned in cases where an unknown address type tries to be parsed viaFromStr
. This provides more context for the error, since it contains theString
that tried to be parsed.Closes #1064