-
Notifications
You must be signed in to change notification settings - Fork 125
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
Allow specifying the error type for NewMiddleware and related traits #430
Conversation
5061728
to
040415f
Compare
040415f
to
cf1d939
Compare
Codecov Report
@@ Coverage Diff @@
## master #430 +/- ##
==========================================
+ Coverage 83.41% 84.21% +0.79%
==========================================
Files 106 106
Lines 5156 5225 +69
==========================================
+ Hits 4301 4400 +99
+ Misses 855 825 -30
Continue to review full report at Codecov.
|
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 definitely see the benefits to this. My only concern with merging ahead of 0.5 final would be the API churn involved. Maybe there aren't enough middleware in the wild to worry about this, and the update seems trivial, generally.
Well, 0.5 can be a breaking change, so if you are happy with the API churn I don't see why this couldn't be merged. Unfortunately associated type defaults aren't stable yet, otherwise we could make this change a non-breaking one. Also, the change to existing middlewares is rather minimal. Most people probably use the derive macro and therefore won't notice. Otherwise, if you don't care about the error type, you'd just add one line: impl Middleware for FooMiddleware {
type Error = io::Error; // the only new line
// existing code
} |
Deserialize, | ||
/// Exhaustive match against this enum is unsupported. | ||
#[doc(hidden)] | ||
#[error("__NonExhaustive")] |
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.
You can simply specify #[non_exhaustive]
on this enum if we bump MSRV to 1.40
One thing I had recently tried to grapple with is that because of the associated types of I understand that To be honest, we don't need the Basically, I am trying to make the usage of middlewares DRY. Currently, it isn't. |
Well, having an associated That said, if gotham doesn't really do anything with the error, especially not returning it to the user, I don't see a reason for not using However, since @nyarly prefers to break less code, I guess it is easier for now to be able to still use |
I don't understand why we don't want to do breaking changes. As long as they are documented, breaking stuff is always preferable because it will improve the library as a whole. |
I though about this a bit further and I think that it actually makes more sense to allign |
Closing this PR in favor of #438 |
I just tried to implement
NewMiddleware
for my own type and realised that I had an error that could occur but was not of typestd::io::Error
. Returningio::Error::Other
along with a custom error message would work but I believe using the?
shorthand is much easier.This is why I introduced
NewMiddleware::Err
type to be specified manually. For most implementations,std::convert::Infallible
is the appropriate error type (this is at least true for all types that use the derive macro), and those that wish to continue returningio::Result
can simply specifytype Err = io::Error
. So this change does not hurt existing implementations but makes future ones much more convenient.