co.easimart.vertx.util.VertxHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vertx-util Show documentation
Show all versions of vertx-util Show documentation
Library provides utility classes for Vertx project development.
The newest version!
package co.easimart.vertx.util;
import com.google.common.util.concurrent.SettableFuture;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
/**
* Groups of helper functions which make regular vertx code such error-handling more readable.
*/
public class VertxHelper {
public static Handler> handleWithSettableFuture(SettableFuture future) {
return res -> {
if (res.failed()) {
future.setException(res.cause());
return;
}
future.set(res.result());
};
}
public static Handler> ifSucceeded(Handler successHandler, Handler> failedHandler) {
return res -> {
if (res.failed()) {
failedHandler.handle(Future.failedFuture(res.cause()));
return;
}
try {
successHandler.handle(res.result());
} catch (Exception ex) {
failedHandler.handle(Future.failedFuture(ex));
}
};
}
public static Handler> ifSucceeded(Handler successHandler, RoutingContext routingContext) {
return res -> {
if (res.failed()) {
routingContext.fail(res.cause());
return;
}
try {
successHandler.handle(res.result());
} catch (Exception ex) {
if (!routingContext.failed()) routingContext.fail(ex);
else throw ex;
}
};
}
}