public static <T extends Throwable> Throwable unwrapExceptions(Class<? extends Throwable> wrapperClassToUnwrap, T t) {
    if (t == null) return null;
    if (wrapperClassToUnwrap == null) return t;

    Throwable originalException = t;
    while (wrapperClassToUnwrap.isAssignableFrom(originalException.getClass()) &&
        originalException.getCause() != null) {
      originalException = originalException.getCause();
    }

    // can still be the wrapper class if no other cause was found.
    if (wrapperClassToUnwrap.isAssignableFrom(originalException.getClass())) {
      return null;
    } else {
      return originalException;
    }
  }
Removes wrapper exceptions of the provided type from the provided throwable and returns the first cause
that does not match the wrapper type. Useful for unwrapping CompletionException / SamzaException
in stack traces and getting to the underlying cause.

Returns null if provided Throwable is null or if there is no cause of non-wrapper type in the stack.
samza-core/src/main/java/org/apache/samza/util/FutureUtil.java