Branching route handler helper
This is useful when building elaborated routes in a programmatic fashion (e.g., predicates).
If you're building routes manually for your API, then yes, I'd do what you did in the second snippet and roll my own handler with a conditional branching structure 😃
Where this gets more complicated is when you are building routes programmatically, and especially when you want branching and not just chains. A good example is when you mount a handler in the chain that doesn't provide any way back / vertx-http-proxy is a good example (and you can't just leverage failure recovery at the router level).
The use-case I have in mind is building predicates, so you can enable a route handler only if certain conditions are met (e.g., some request header is present, etc). Having such a building block is helpful, especially when you add routes programmatically.
A good example is when you mount a handler in the chain that doesn't provide any way back
Wouldn't this do the trick?
Handler<RoutingContext> handler = createMyAwesomeHandler();
router.get("/branching").handler(rc -> {
if (myConditionIsMet()) {
handler.handle(rc);
}
else {
rc.next();
}
});
It does, but having a building block like this is also a viable route
It does, but having a building block like this is also a viable route
Not sure what you mean with "like this". Did you mean like the proposed BranchingHandler? In this case, the following code:
Handler<RoutingContext> handler = createMyAwesomeHandler();
router.get("/branching").handler(BranchingHandler.create(
rc -> myConditionIsMet(),
handler::handle
));
is not much more concise or readable than:
Handler<RoutingContext> handler = createMyAwesomeHandler();
router.get("/branching").handler(rc -> {
if (myConditionIsMet()) {
handler.handle(rc);
} else {
rc.next();
}
});
Sorry if I'm missing something obvious.
I'm not looking for conciseness but composability.