public static <T extends Throwable> Optional<T> findThrowable(
            Throwable throwable, Class<T> searchType) {
        if (throwable == null || searchType == null) {
            return Optional.empty();
        }

        Throwable t = throwable;
        while (t != null) {
            if (searchType.isAssignableFrom(t.getClass())) {
                return Optional.of(searchType.cast(t));
            } else {
                t = t.getCause();
            }
        }

        return Optional.empty();
    }
@Test
    void testFindThrowableByType() {
        assertThat(
                        ExceptionUtils.findThrowable(
                                new RuntimeException(new IllegalStateException()),
                                IllegalStateException.class))
                .isPresent();
    }