private void validateOrThrow(RxDocumentServiceRequest request, HttpResponseStatus status, HttpResponseHeaders headers, InputStream inputStream) throws DocumentClientException { int statusCode = status.code(); if (statusCode >= HttpConstants.StatusCodes.MINIMUM_STATUSCODE_AS_ERROR_GATEWAY) { String body = null; if (inputStream != null) { try { body = IOUtils.toString(inputStream, StandardCharsets.UTF_8); } catch (ParseException | IOException e) { logger.error("Failed to get content from the http response", e); throw new IllegalStateException("Failed to get content from the http response", e); } finally { IOUtils.closeQuietly(inputStream); } } Map<String, String> responseHeaders = new HashMap<String, String>(); for (Entry<String, String> header : headers.entries()) { responseHeaders.put(header.getKey(), header.getValue()); } String statusCodeString = status.reasonPhrase() != null ? status.reasonPhrase().replace(" ", "") : ""; Error error = null; error = (body != null)? new Error(body): new Error(); error = new Error(statusCodeString, String.format("%s, StatusCode: %s", error.getMessage(), statusCodeString), error.getPartitionedQueryExecutionInfo()); throw new DocumentClientException(statusCode, error, responseHeaders); } }