private static <T> Mono<T> doDecode(ClientResponse response, Class<T> responseType, String content) { String mediaType = response.headers().contentType().map(MediaType::toString).orElse(XContentType.JSON.mediaType()); try { Method fromXContent = ReflectionUtils.findMethod(responseType, "fromXContent", XContentParser.class); return Mono.justOrEmpty(responseType .cast(ReflectionUtils.invokeMethod(fromXContent, responseType, createParser(mediaType, content)))); } catch (Throwable errorParseFailure) { // cause elasticsearch also uses AssertionError try { return Mono.error(BytesRestResponse.errorFromXContent(createParser(mediaType, content))); } catch (Exception e) { return Mono .error(new ElasticsearchStatusException(content, RestStatus.fromCode(response.statusCode().value()))); } } }
if (token != XContentParser.Token.FIELD_NAME) { ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser::getTokenLocation); status = RestStatus.fromCode(parser.intValue());
status = RestStatus.fromCode(parser.intValue());
return new Failure(index, type, id, bulkExc, RestStatus.fromCode(status)); } else if (searchExc != null) { return new SearchFailure(searchExc, index, shardId, nodeId);
/** * Converts a {@link ResponseException} obtained from the low level REST client into an {@link ElasticsearchException}. * If a response body was returned, tries to parse it as an error returned from Elasticsearch. * If no response body was returned or anything goes wrong while parsing the error, returns a new {@link ElasticsearchStatusException} * that wraps the original {@link ResponseException}. The potential exception obtained while parsing is added to the returned * exception as a suppressed exception. This method is guaranteed to not throw any exception eventually thrown while parsing. */ protected final ElasticsearchStatusException parseResponseException(ResponseException responseException) { Response response = responseException.getResponse(); HttpEntity entity = response.getEntity(); ElasticsearchStatusException elasticsearchException; if (entity == null) { elasticsearchException = new ElasticsearchStatusException( responseException.getMessage(), RestStatus.fromCode(response.getStatusLine().getStatusCode()), responseException); } else { try { elasticsearchException = parseEntity(entity, BytesRestResponse::errorFromXContent); elasticsearchException.addSuppressed(responseException); } catch (Exception e) { RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode()); elasticsearchException = new ElasticsearchStatusException("Unable to parse response body", restStatus, responseException); elasticsearchException.addSuppressed(e); } } return elasticsearchException; }
/** * Wrap the ResponseException in an exception that'll preserve its status code if possible so we can send it back to the user. We might * not have a constant for the status code so in that case we just use 500 instead. We also extract make sure to include the response * body in the message so the user can figure out *why* the remote Elasticsearch service threw the error back to us. */ static ElasticsearchStatusException wrapExceptionToPreserveStatus(int statusCode, @Nullable HttpEntity entity, Exception cause) { RestStatus status = RestStatus.fromCode(statusCode); String messagePrefix = ""; if (status == null) { messagePrefix = "Couldn't extract status [" + statusCode + "]. "; status = RestStatus.INTERNAL_SERVER_ERROR; } try { return new ElasticsearchStatusException(messagePrefix + bodyMessage(entity), status, cause); } catch (IOException ioe) { ElasticsearchStatusException e = new ElasticsearchStatusException(messagePrefix + "Failed to extract body.", status, cause); e.addSuppressed(ioe); return e; } }
if ((token = parser.nextToken()) != Token.FIELD_NAME) { ensureExpectedToken(Token.VALUE_NUMBER, token, parser::getTokenLocation); status = RestStatus.fromCode(parser.intValue());
if ((token = parser.nextToken()) != Token.FIELD_NAME) { ensureExpectedToken(Token.VALUE_NUMBER, token, parser::getTokenLocation); status = RestStatus.fromCode(parser.intValue());
if (token != XContentParser.Token.FIELD_NAME) { ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser::getTokenLocation); status = RestStatus.fromCode(parser.intValue());
if (token != XContentParser.Token.FIELD_NAME) { ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser::getTokenLocation); status = RestStatus.fromCode(parser.intValue());
if (token != XContentParser.Token.FIELD_NAME) { ensureExpectedToken(XContentParser.Token.VALUE_NUMBER, token, parser::getTokenLocation); status = RestStatus.fromCode(parser.intValue());
status = RestStatus.fromCode(parser.intValue());
status = RestStatus.fromCode(parser.intValue());
status = RestStatus.fromCode(parser.intValue());
return new Failure(index, type, id, bulkExc, RestStatus.fromCode(status)); } else if (searchExc != null) { return new SearchFailure(searchExc, index, shardId, nodeId);