czerny
czerny

Reputation: 16664

Vertx: How to get fail() status code in failure handler?

How can status code passed to RoutingContext.fail(int) be retrieved in failure handler (registered using Route.failureHandler)?

E.g. If processing of some route takes too long, it can be interrupted by TimeoutHandler that calls fail(408) on RoutingContext. fail method subsequently invokes failure handler (if registered). However I can't find any way (except for reflection & depending on implementation) how to find out in handler itself why it was called:

Upvotes: 3

Views: 3817

Answers (2)

NoamK
NoamK

Reputation: 61

Not sure about older version of Vert.x, but from 3.0.0 an up:

eventBus.send("some.address", "some value", event -> {
  if (event.succeeded()) {
    System.out.println("Received reply: " + event.result().body());
  } else {
    ReplyException cause = (ReplyException) event.cause();
    String failMessage = cause.getMessage();
    int failCode = cause.failureCode();
  }
});

Upvotes: 6

Paulo Lopes
Paulo Lopes

Reputation: 5801

You should be able to see the error code as there is a unit test for that specific scenario:

router.route(path).handler(rc -> {
  rc.fail(400);
}).failureHandler(frc -> {
  assertEquals(400, frc.statusCode());
  frc.response().setStatusCode(400).setStatusMessage("oh dear").end();
});

How are you registering your handler? and failure handler?

Upvotes: 3

Related Questions