All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.easimart.vertx.util.VertxHelper Maven / Gradle / Ivy

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;
            }
        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy