-
Notifications
You must be signed in to change notification settings - Fork 14
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
Switch to snafu
as error handling library
#50
Conversation
4c07fee
to
e6c8e62
Compare
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.
This all looks good to me! It think the only substantive comments were about removing From
implementations in favor of using #[snafu(context(false))]
on variants.
I am not familiar with using Snafu in combination with anyhow. I was under the impression that they were separate, orthogonal error handling strategies:
With Snafu, you have an error enum with different variants, and snafu helps you map underlying errors into your error type, implements display, and other helpful things like that.
Anyhow, on the other hand, provides a bunch of helpers for basically returning Box<dyn std::error::Error>
everywhere, mapping errors too that type, and adding underlying errors.
However, I think I might have been mistaken. Is that not the case? With the code in the current PR, do bendy users still concrete bendy error types that they can match on specific error variants?
} | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "std"))] | ||
impl From<state_tracker::StructureError> for Error { |
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.
This can be removed if you add context(false)
to the variant.
pub struct Error(#[fail(cause)] pub ErrorKind); | ||
#[derive(Debug, Clone, Snafu)] | ||
#[snafu(display("encoding failed: {}", source))] | ||
pub struct Error { |
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 think it's unrelated to this PR, but is there a reason that there are separate Error
and ErrorKind
types? Is it for future flexibility in case you want to add things to the Error
type?
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.
As far as I remember the idea was to come up with an error type that allows us to add additional variants without any concerns about potential compatibility issues. I know this sometimes increases the workload to actualy handle a specifc error.
Do you think its worth to discuss this decision?
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 usually see this pattern used when you have an type with many variants, but there are fields with are common to all variants, so instead of doing:
enum Foo {
A { id: u32 },
B { id: u32 },
C { id: u32 },
}
You do:
struct Foo {
id: u32,
kind: FooKind,
}
enum FooKind {
A,
B,
C,
}
It stood out to me in this case because doesn't seem to be any shared data between variants. I don't think it's done to allow backwards compatible extension with new variants, usually that's done by marking the enum as #[non_exhaustive]
.
But, if you might add shared data between variants, or if you don't mind a breaking change in the future, then I think this isn't critical to address.
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 can see how #[non_exhaustive]
would allow to simplify everything. I could rewrite the structure but I assume thats something @thequux should decide.
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.
Yeah, I think that #[non_exhaustive]
makes more sense here than the ErrorKind structure. IIRC, we used the ErrorKind split because it was recommended in https://rust-lang-nursery.github.io/failure/error-errorkind.html, and we didn't think too closely about it at the time.
/// Error in the bencode structure (e.g. a missing field end separator). | ||
#[fail(display = "bencode encoding corrupted")] | ||
StructureError(#[fail(cause)] StructureError), | ||
#[snafu(display("bencode encoding corrupted"))] |
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 think this can get context(false)
too.
impl From<StructureError> for Error { | ||
fn from(error: StructureError) -> Self { | ||
Self(ErrorKind::StructureError(error)) | ||
impl From<state_tracker::StructureError> for Error { |
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.
This can be deleted if the variant gets context(false)
Thanks for the review :) I am going to address your concerns tomorrow!
AFAIK you are right. The strategy of The only reason I introduced
This should still be possible as any internal method returns either |
Ah, okay, I was confused, I didn't notice that anyhow was being used in an example. I think if this is the only use of |
This PR has been sitting here for a few months now, and I'm generally in favor of the direction that it moves the crate. For that reason, I'm going to merge it. However, I don't think that it is complete. @casey's comments on places where we could use I'll work on fixing those remaining issues in the next day or two, so expect some more PRs :-) |
This is my first attempt to replace
failure
withsnafu
asfailure
got officially deprcated. It should finally fix #40. Even if it seems like the CI doesn't like this changeset... Travis CI just stucks and on GHA the macos-latest builder are not able to downloadsnafu_derive
...@casey could you review this? I hadn't much chance to program rust during this year and I assume @thequux is still quite busy. Feel free to nitpick as much as you like.