-
Notifications
You must be signed in to change notification settings - Fork 321
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
Rework middleware to receive Response directly, add optional errors to Response #570
Conversation
@Fishrock123 i added a commit at Fishrock123#1 that gets this pr compiling |
5d9b539
to
a1cb10e
Compare
Here is what this could enable in the future once the |
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.
Overall this is looking really good! -- left a few nits; but this is def in the right direction. Thanks so much @Fishrock123!
examples/error_handling.rs
Outdated
tide::log::start(); | ||
let mut app = tide::new(); | ||
|
||
app.middleware(After(|mut res: Response| async move { |
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.
should After
take both request
and response
? For example I might want to read a request headers and modify the response as appropriate. One example is taking the client request header from request and setting it to response header.
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.
Unfortunately that's not possible currently with After. If you need access to the request and the response, you'll need to write a full middleware. It would be misleading for After to provide a request, because it would be a clone, and potentially the "real" request could have changed after the clone was made, but that would be obfuscated by the After.
a1cb10e
to
f2d1608
Compare
|
7c9e426
to
9fd5722
Compare
This allows errors to be propogated through the Tide middleware stack while still keeping an existing Response intact. Effectively allowing headers, body, etc to be passed along with an Error; while also opening up options for nicer error handling via `res.downcast_error<>` checking. Also contains APIs for turning an Error into a Response via Into, and taking the error from / setting an error on an existing Response. This is a breaking change in that `next.run().await` within middleware no longer returns a tide::Result but rather always returns a tide::Response. Thanks to Jacob Rothstein for helping me get this to compile! PR-URL: http-rs#570
a031ad3
to
cd9568a
Compare
I believe this is just waiting on downstream releases now. I've rebased the commits nicely. |
This allows errors to be propogated through the Tide middleware stack while still keeping an existing Response intact. Effectively allowing headers, body, etc to be passed along with an Error; while also opening up options for nicer error handling via `res.downcast_error<>` checking. Also contains APIs for turning an Error into a Response via Into, and taking the error from / setting an error on an existing Response. This is a breaking change in that `next.run().await` within middleware no longer returns a tide::Result but rather always returns a tide::Response. Thanks to Jacob Rothstein for helping me get this to compile! PR-URL: http-rs#570
cd9568a
to
d5e4e66
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.
One last comment, but otherwise this is looking 💯
Addressed that, removed |
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 approach seems better for middleware. Thanks!
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.
Patch LGTM!
http-rs/http-types#191 however was recently reverted, and this patch needs to be updated to bring the error types into Tide itself. That should allow us to move off the git deps, and allow us to merge. Excited for this!
This allows errors to be propagated through the Tide middleware stack while still keeping an existing Response intact. Effectively allowing headers, body, etc to be passed along with an Error; while also opening up options for nicer error handling via `res.downcast_error<>` checking. Also contains APIs for turning an Error into a Response via Into, and taking the error from / setting an error on an existing Response. This is a breaking change in that `next.run().await` within middleware no longer returns a tide::Result but rather always returns a tide::Response. Thanks to Jacob Rothstein for helping me get this to compile! PR-URL: http-rs#570
e663833
to
9bf7c09
Compare
This allows errors to be propagated through the Tide middleware stack while still keeping an existing Response intact. Effectively allowing headers, body, etc to be passed along with an Error; while also opening up options for nicer error handling via `res.downcast_error<>` checking. Also contains APIs for turning an Error into a Response via Into, and taking the error from / setting an error on an existing Response. This is a breaking change in that `next.run().await` within middleware no longer returns a tide::Result but rather always returns a tide::Response. Thanks to Jacob Rothstein for helping me get this to compile! PR-URL: http-rs#570
Makes use of the newly introduced error propgation from `Response: add optional error storage` to show a nice and flexible error handler middleware approach. Related to http-rs#549
Makes use of the newly updated error propgation from `Response: add optional error storage` for a more informative error case in the log middleware.
This is probably never what a Tide user wants. The developer of a server can use the log middleware, otherwise this presents a risk as it may point to potentially exploitable flaws. Additionally, Tide can't completely aguarentee this will be in an expected format.
9bf7c09
to
2c59a63
Compare
Rebased and pulled the reverted logic from |
Rework middleware so that:
next.run().await
always returns aResponse
.tide::Result
is always transformed into aResponse
with error set.Adds the following things to
Response
:pub fn error(&self) -> Option<&Error>
pub fn downcast_error<E>(&self) -> Option<&E>
pub fn take_error(&mut self) -> Option<Error>
impl From<Error> for Response
impl From<crate::Result> for Response
An example of this being used for an error handling middleware is included at
examples/error_handling.rs
, and Jacob has transformed someone else's in #452 (comment).Dependencies:
[x] Merge Response: add Error storage, retrieval, conversion http-types#174 (ABI breaking)[ ]http-types
release[ ]http-client
release[ ] Surf releaseNote: Please let me rebase before merging