class ThrowableUtils {

  public static void setRootCause(Throwable exception, Throwable newRootCause) {
    Throwable nt = exception;
    while (nt.getCause() != null) {
      nt = nt.getCause();
    }

    // Don't try to set something as it's own cause
    if (nt == newRootCause) {
      return;
    }

    nt.initCause(newRootCause);
  }

}