public static Optional<Throwable> findThrowableInChain(Predicate<Throwable> condition, @Nullable Throwable t) {
        final Set<Throwable> seen = new HashSet<>();
        while (t != null && !seen.contains(t)) {
            if (condition.test(t)) {
                return Optional.of(t);
            }
            seen.add(t);
            t = t.getCause();
        }
        return Optional.empty();
    }
@Test
    void findsSimpleException() {
        final RuntimeException e = new RuntimeException();

        assertThat(findThrowableInChain(t -> t instanceof RuntimeException, e)).contains(e);
        assertThat(findThrowableInChain(t -> false, e)).isEmpty();
    }