vert-x3 / vertx-service-proxy

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

Cannot set cause on ServiceException #152

Open ben1222 opened 5 days ago

ben1222 commented 5 days ago

Version

4.5.11

Context

We have a subclass of ServiceException that uses initCause to set the cause of the ServiceException and have a customized codec to encode/decode the cause. However, after recent change in ReplyException (which the ServiceException extends) in vertx 4.5.11: https://github.com/eclipse-vertx/vert.x/issues/5335 , we can no longer use initCause for it because the constructor public ReplyException(ReplyFailure failureType, int failureCode, String message) which the ServiceException calls now init the cause with null.

The ServiceException may need to also provide a protected method with cause that allows sub-class to set the cause on it, to match ReplyException.

Do you have a reproducer?

A small unit test that illustrates the problem:

public static class TestServiceException extends ServiceException {
  public TestServiceException(int failureCode, String message, Throwable cause) {
    super(failureCode, message);
    initCause(cause);
  }
}

@Test
public void testServiceExceptionWithCause() {
  Throwable cause = new NullPointerException("test cause");
  ServiceException serviceException = new TestServiceException(123, "abc", cause);

  assertSame(serviceException.getCause(), cause);
}

This test case passed on vertx 4.5.10 but failed on vertx 4.5.11:

java.lang.IllegalStateException: Can't overwrite cause with java.lang.NullPointerException: test cause

    at java.base/java.lang.Throwable.initCause(Throwable.java:462)
...
Caused by: (RECIPIENT_FAILURE,123) abc
ben1222 commented 5 days ago

Even if the ServiceException provides a constructor that takes a cause, it still lost the ability to set the cause after the exception is created... There are some of such use cases that is inconvenient to pass the cause to the constructor...

But I do not have a better solution... perhaps I have to take the inconvenient way to pass the cause to the constructor...

ben1222 commented 3 days ago

@vietj could you take a look?