vert-x3 / vertx-service-proxy

EventBus Proxy generation
Apache License 2.0
67 stars 58 forks source link

Reply with an exception without losing stack trace #88

Closed ruslangm closed 5 years ago

ruslangm commented 5 years ago

I'm using generated VertxProxyHandler class for my service (SomeServiceVertxProxyHandler. class). When delivering a message through an event bus (in the case that an exception occurred during the execution), I get another exception that wraps my real one and thus losing the stacktrace. The main problem is in this code:

 public void handle(Message<JsonObject> msg) {
    try{
      JsonObject json = msg.body();
      String action = msg.headers().get("action");
      if (action == null) throw new IllegalStateException("action not specified");
      accessed();
      switch (action) {
        case "getStart": {
          service.getStart((java.lang.String)json.getValue("tmsec"),
                        HelperUtils.createListHandler(msg));
          break;
        }
...

Look at the HelperUtils.createListHandler(msg) method:

public static <T> Handler<AsyncResult<List<T>>> createListHandler(Message msg) {
    return res -> {
      if (res.failed()) {
        if (res.cause() instanceof ServiceException) {
          msg.reply(res.cause());
        } else {
          msg.reply(new ServiceException(-1, res.cause().getMessage()));
        }

If res.cause() instance of Exception (any kind of exception that may happens) and not contains message of the cause (as in case when you pass null in Objects.requireNonNull()) you will get completely another stacktrace with completely not informative message.

May be it will be better to call initCause(Throwable t) or addSuppressed(Throwable t) to not losing original exception?

slinkydeveloper commented 5 years ago

Created a pr #90