Do function execution continues after failing Promise in Java Vertx

137 views Asked by At

I have a function which I'm trying to reduce complexity of.

public Future<JsonArray> getAllCompanies() {

        String url = ...

        return Future.future(
                promise -> webClient.getAbs(url).putHeader(String.valueOf(HttpHeaders.ACCEPT), APPLICATION_JSON)
                .putHeader(String.valueOf(HttpHeaders.CONTENT_TYPE), APPLICATION_JSON)
                .basicAuthentication(username, password).send(ar -> {
                    if (ar.succeeded()) {
                        HttpResponse<Buffer> response = ar.result();
                        if (response.statusCode() == 200 || response.statusCode() == 201) {
                            logger.info("getAllCompanies API call succeeded with code 200");
                            JsonObject json = null;
                            try {
                                json = response.bodyAsJsonObject();
                            } catch (DecodeException e) {
                                logger.error(ERROR_GETTING_RESPONSE_BODY_AS_JSON, e);
                                logger.error(response.bodyAsString());
                                promise.fail(JSON_DECODE_FAILED);
                            }
                            if (json != null) {
                                JsonArray results = json.getJsonArray(RESULT);

                                logger.info("FINISHED! Returned {} companies", results.size());
                                promise.complete(results);
                            }
                        } else {
                            logger.error("getAllCompanies call failed with HTTP status code: {}",
                                    response.statusCode());
                            logger.error(response.bodyAsString());
                            promise.fail(HTTP_STATUS_CODE_FAILURE);
                        }
                    } else {
                        logger.error("getAllCompanies call failed to complete!", ar.cause());
                        promise.fail(HTTP_REQUEST_FAILED);
                    }
                }));
    }

And I'm doing it by bringing false conditions to the top and removing else block which brings down the nested code.

This is the updated code:

public Future<JsonArray> getAllCompanies() {

        String url = ...

        return Future.future(
                promise -> webClient.getAbs(url).putHeader(String.valueOf(HttpHeaders.ACCEPT), APPLICATION_JSON)
                .putHeader(String.valueOf(HttpHeaders.CONTENT_TYPE), APPLICATION_JSON)
                .basicAuthentication(username, password).send(ar -> {
                    if (!ar.succeeded()) {
                        logger.error("getAllCompanies call failed to complete!", ar.cause());
                        promise.fail(HTTP_REQUEST_FAILED);
                    }
                    HttpResponse<Buffer> response = ar.result();
                    if (response.statusCode() != 200 && response.statusCode() != 201) {
                        logger.error("getAllCompanies call failed with HTTP status code: {}",
                                response.statusCode());
                        logger.error(response.bodyAsString());
                        promise.fail(HTTP_STATUS_CODE_FAILURE);
                    }
                    logger.info("getAllCompanies API call succeeded with code 200");
                    JsonObject json = null;
                    try {
                        json = response.bodyAsJsonObject();
                    } catch (DecodeException e) {
                        logger.error(ERROR_GETTING_RESPONSE_BODY_AS_JSON, e);
                        logger.error(response.bodyAsString());
                        promise.fail(JSON_DECODE_FAILED);
                    }
                    if (json != null) {
                        JsonArray results = json.getJsonArray(RESULT);

                        logger.info("FINISHED! Returned {} companies", results.size());
                        promise.complete(results);
                    }
                }));
    }

If the program comes inside the updated code's first if condition !ar.succeeded() and promise.fail is called promise.fail(HTTP_REQUEST_FAILED); Do program continues or just returns it? Because I have removed else block, I don't want it to continute, perhaps I use return; statement? I would like to know how Vertx Future and promise works!

1

There are 1 answers

0
gdomo On

Well, java executes each line of a method one by one until 1) return called 2) an exception is thrown 3) method ends.

Since promise.fail just sets the failure to the promise it doesn't meet any of 3 conditions, so you program would be executed further. Though, you could easily test it by yourself.