Search in sources :

Example 1 with RestClientException

use of org.apache.flink.runtime.rest.util.RestClientException in project flink by apache.

the class RestClient method parseResponse.

private static <P extends ResponseBody> CompletableFuture<P> parseResponse(JsonResponse rawResponse, JavaType responseType) {
    CompletableFuture<P> responseFuture = new CompletableFuture<>();
    final JsonParser jsonParser = objectMapper.treeAsTokens(rawResponse.json);
    try {
        P response = objectMapper.readValue(jsonParser, responseType);
        responseFuture.complete(response);
    } catch (IOException originalException) {
        // lets see if it is an ErrorResponse instead
        try {
            ErrorResponseBody error = objectMapper.treeToValue(rawResponse.getJson(), ErrorResponseBody.class);
            responseFuture.completeExceptionally(new RestClientException(error.errors.toString(), rawResponse.getHttpResponseStatus()));
        } catch (JsonProcessingException jpe2) {
            // if this fails it is either the expected type or response type was wrong, most
            // likely caused
            // by a client/search MessageHeaders mismatch
            LOG.error("Received response was neither of the expected type ({}) nor an error. Response={}", responseType, rawResponse, jpe2);
            responseFuture.completeExceptionally(new RestClientException("Response was neither of the expected type(" + responseType + ") nor an error.", originalException, rawResponse.getHttpResponseStatus()));
        }
    }
    return responseFuture;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RestClientException(org.apache.flink.runtime.rest.util.RestClientException) ErrorResponseBody(org.apache.flink.runtime.rest.messages.ErrorResponseBody) IOException(java.io.IOException) JsonProcessingException(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException) JsonParser(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser)

Aggregations

IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ErrorResponseBody (org.apache.flink.runtime.rest.messages.ErrorResponseBody)1 RestClientException (org.apache.flink.runtime.rest.util.RestClientException)1 JsonParser (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParser)1 JsonProcessingException (org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonProcessingException)1