docs(examples): add custom either middleware example#709
Open
Isvane wants to merge 3 commits into
Open
Conversation
Isvane
force-pushed
the
docs/add-custom-either-mid-example
branch
from
July 6, 2026 11:57
212c692 to
116970d
Compare
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.
Description
Hey! Opening this PR to add a new example showing how to handle conditional, short-circuiting responses in custom
towermiddleware.I’ve set it up under
examples/custom-middleware-eitherwith its ownCargo.toml,README.md, andsrc/main.rsto match the layout of the existing examples.Note: The code is working but a bit rough around the edges, and my doc comments and the
README.mdare definitely lacking right now. I couldn't quite find the right words to make it super clear. I'd love some feedback on both the wording and the code during the review.Motivation
This comes from a recent Discord discussion from Tony, Darius and @jlizen. Because
Serviceforces you to have one concreteResponseassociated type, you run into a wall if your middleware wants to either bounce a request early (with an error body) or let it pass through to the inner service (with whatever body that service returns)Solution
I added the custom-middleware-either example. Which contains:
Either<B, Full<Bytes>>to gracefully unify the happy-path body and the early-rejection error body.std::future::readyfor the error path.