public static SearchTypeError parse(Query query, String searchTypeId, ElasticsearchException ex) {

        if (isSearchTypeAbortedError(ex)) {
            return new SearchTypeAbortedError(query, searchTypeId, ex);
        }

        Throwable possibleResultWindowException = ex;
        int attempt = 0;
        while (possibleResultWindowException != null && attempt < MAX_DEPTH_OF_EXCEPTION_CAUSE_ANALYSIS) {
            final Integer resultWindowLimit = parseResultLimit(possibleResultWindowException);
            if (resultWindowLimit != null) {
                return new ResultWindowLimitError(query, searchTypeId, resultWindowLimit);
            }
            possibleResultWindowException = possibleResultWindowException.getCause();
            attempt++;
        }

        return new SearchTypeError(query, searchTypeId, ex);
    }
@Test
    void returnsResultWindowLimitErrorIfPresentInTheExceptionsCauseChain() {
        final ElasticsearchException elasticsearchException = new ElasticsearchException(
                "Something is wrong!",
                new IllegalStateException(
                        "Run for your lives!!!",
                        new ElasticsearchException("Result window is too large, [from + size] must be less than or equal to: [42]")
                ));
        final SearchTypeError error = SearchTypeErrorParser.parse(query, "searchTypeId", elasticsearchException);

        assertThat(error).isInstanceOf(ResultWindowLimitError.class);

        assertThat((ResultWindowLimitError) error)
                .satisfies(e -> assertEquals(42, e.getResultWindowLimit()))
                .satisfies(e -> assertEquals("searchTypeId", e.searchTypeId()))
                .satisfies(e -> assertEquals("test_query", e.queryId()));
    }