Future has already synchronous operations modifying the control flow
map : transforms a mapper failure into a failed future (e.g. future.map(val -> throw new VertxException()))
otherwise : transforms a failure into a success
This contribution aims to transform a success into a failure based on the result of a predicate.
Goals
capture useful predicates that modify the control flow, e.g. a predicate that checks the HTTP client response (status code, headers, etc...)
provide inline guards using lambda expressions
Vert.x Web Client has something close to this with HTTP expectations:
client
.get(8080, "myserver.mycompany.com", "/some-uri")
.expect(ResponsePredicate.SC_SUCCESS)
.expect(ResponsePredicate.JSON)
.send()
.onSuccess(res -> {
// Safely decode the body as a json object
JsonObject body = res.bodyAsJsonObject();
System.out.println(
"Received response with status code" +
res.statusCode() +
" with body " +
body);
})
.onFailure(err ->
System.out.println("Something went wrong " + err.getMessage()));
A predicate like interface Expectation: a synchronous predicate that checks a boolean value and has a method to create a meaningful exception.
Future
control flow using predicates.Future
has already synchronous operations modifying the control flowmap
: transforms a mapper failure into a failed future (e.g.future.map(val -> throw new VertxException())
)otherwise
: transforms a failure into a successThis contribution aims to transform a success into a failure based on the result of a predicate.
Goals
Vert.x Web Client has something close to this with HTTP expectations:
A predicate like interface
Expectation
: a synchronous predicate that checks a boolean value and has a method to create a meaningful exception.The HTTP response status code can be captured so it can be reused.