Orkun
Orkun

Reputation: 7228

vertx failed future but treated as succeeded()

I have a method:

    public void getVmsAdminToken(HttpClient httpClient, handler<AsyncResult<String>> handler) {
           httpClient.postAbs(url, h -> h.bodyHandler(bh -> {
                try {
                    switch (h.statusCode()) {
                        case 200:
                            JsonObject vmsResponse = bh.toJsonObject();                       
                            handler.handle(Future.succeededFuture(Json.encode(vmsResponse)));
                            break;
                        default:
                            LOG.error("VMS call failed {}", h.statusCode());
                            handler.handle(Future.failedFuture(500 + ""));
                            break;
                    }
                } catch (Throwable t) {
                    LOG.error("Exception in getVmsAdminToken", t);
                    handler.handle(Future.failedFuture(500 + ""));
                }
            }))
                    .setTimeout(timeOutMs)
                    .putHeader("content-type", "application/json")
                    .putHeader("stub", apiKey)
                    .end(vehicleReqBody.encode());
    }

I use this inside the following method call :

private void getAdminToken(RoutingContext ctx, RedisFleetStorage storage, HttpClient httpClient) {

    getVmsAdminToken(fleetId, user, vehicle, httpClient, replyVms -> {
                                            if (reply.succeeded()) {
                                               // why succeeded?!!
                                            }
                                        });
}

And even if the getVmsToken fails, the execution falls into the if (reply.succeeded())

Why might that be?

Upvotes: 0

Views: 725

Answers (1)

tmarwen
tmarwen

Reputation: 16354

You should check the same AsyncResult object being the result of your HTTP call:

private void getAdminToken(RoutingContext ctx, RedisFleetStorage storage, HttpClient httpClient) {

    getVmsAdminToken(fleetId, user, vehicle, httpClient, replyVms -> {
                                            if (replyVms.succeeded()) {
                                               // do you thing
                                            }
                                        });
}

Upvotes: 1

Related Questions