private Mono handleErrorResponse()

in data/src/main/java/com/microsoft/azure/kusto/data/BaseClient.java [97:129]


    private Mono<InputStream> handleErrorResponse(HttpResponse httpResponse, ResponseState state, HttpRequest request,
            long timeoutMs, int currentRedirectCounter, int maxRedirectCount) {
        return Utils.getResponseBody(httpResponse)
                .flatMap(content -> {
                    state.setErrorFromResponse(content);
                    if (content.isEmpty() || content.equals("{}")) {
                        throw new DataServiceException(request.getUrl().toString(),
                                "postToStreamingOutputAsync failed to get or decompress response body.",
                                true);
                    }

                    // Ideal to close here (as opposed to finally) so that if any data can't be flushed, the exception will be properly thrown and
                    // handled
                    httpResponse.close();

                    if (shouldPostToOriginalUrlDueToRedirect(httpResponse.getStatusCode(), currentRedirectCounter, maxRedirectCount)) {
                        Optional<HttpHeader> redirectLocation = Optional.ofNullable(httpResponse.getHeaders().get(HttpHeaderName.LOCATION));

                        return redirectLocation
                                .filter(location -> !location.getValue().equals(request.getUrl().toString()))
                                .map(location -> {
                                    HttpRequest redirectRequest = HttpRequestBuilder
                                            .fromExistingRequest(request)
                                            .withURL(location.getValue())
                                            .build();
                                    return postToStreamingOutputAsync(redirectRequest, timeoutMs, currentRedirectCounter + 1, maxRedirectCount);
                                })
                                .orElseThrow(() -> createExceptionFromResponse(request.getUrl().toString(), httpResponse, null, state.getErrorFromResponse()));
                    }

                    throw createExceptionFromResponse(request.getUrl().toString(), httpResponse, null, state.getErrorFromResponse());
                });
    }