[vert.x-3] Eventbus Exception Handling

1,085 views
Skip to first unread message

Nico Huysamen

unread,
Nov 7, 2015, 3:57:12 AM11/7/15
to vert.x
Not sure if I am missing something obvious, but I cannot seem to get exception handling to work properly.  Currently, the flow is as follows:

We have a verticle acting as a REST entry point into the system.  It has a HttpServer, Router etc.  This works fine.  Once a request comes in, it translates it into an event bus message, and sends off the message, which will then be picked up by the appropriate verticle listening on the address.

However, we would like to deal with unchecked exceptions in a uniform way, i.e. ending the RoutingContext with a proper JSON response and error message.  So in the web server verticle, we do the following:


vertx.eventBus().send("/eb/some/address", new JsonObject().put("my", "data"), reply -> {
    if (reply.succeeded()) {
        context.response().end("well done");
    } else {
        context.response().end(new MyUniformErrorResponse(reply.cause()).toString());
    }
});


So clearly in both cases we can end the context properly.  Now, we have a verticle listening on the address, and doing the following:


vertx.eventBus().<JsonObject>.consumer("/eb/some/address", this::handleSomeAddress).exceptionHandler(this::handleException);

private void handleSomeAddress(final Message<JsonObject> message) {
    throw new IllegalStateException("o my...");
}

private void handleException(final Throwable throwable) {
    // this never gets called?
}


Our issue is now this, the method registered on the exception handler never gets called. Also, the message is never replied to (would have expected the message to auto reply with a failed response), which in turn causes the error received in the web server verticle message being a normal timeout exception.  I would have expected the exception handler method to be called for unchecked exceptions.  Surely we should not have to wrap every consumer handler in a try-catch just to catch any unchecked exceptions.

Am I missing something?  Or is there a better way to universally deal with exceptions in Vert.x 3?

Julien Viet

unread,
Nov 7, 2015, 4:20:38 AM11/7/15
to ve...@googlegroups.com, Nico Huysamen
Hi,

in the first case, you get an exception on a *reply*.

in the second case you have a message consumer, it is not clear what kind of exceptions would you expect. On a message consumer the exceptionHandler would rather signal an error with respect to the subscription to the event bus and not an error in messages themselves.

-- 
Julien Viet
www.julienviet.com

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/d77a7797-5720-40e9-83d1-51969cdcf860%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nico Huysamen

unread,
Nov 7, 2015, 4:30:17 AM11/7/15
to vert.x, nico.h...@addynamo.com
Hi Julien

Yeah, I guessed as much.  The exception on subscription part is a bit unclear (I don't think I have seen this described in the docs either).  Basically what I am looking for is something the like JEE ExceptionMapper stuff.  Is there any way to do this nicely in Vert.x?

Thanks

Julien Viet

unread,
Nov 7, 2015, 6:35:07 AM11/7/15
to ve...@googlegroups.com, Nico Huysamen, nico.h...@addynamo.com
actually the exceptionHandler on MessageConsumer does nothing at the moment, it is here because it extends ReadStream<Message<T>> and exception handling is a generic thing in this interface.

-- 
Julien Viet
www.julienviet.com

Reply all
Reply to author
Forward
0 new messages