Search in sources :

Example 6 with ElasticsearchStatusException

use of org.elasticsearch.ElasticsearchStatusException in project elasticsearch by elastic.

the class RemoteScrollableHitSource method wrapExceptionToPreserveStatus.

/**
     * 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;
    }
}
Also used : RestStatus(org.elasticsearch.rest.RestStatus) IOException(java.io.IOException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 7 with ElasticsearchStatusException

use of org.elasticsearch.ElasticsearchStatusException in project elasticsearch by elastic.

the class ReindexFromRemoteWithAuthTests method testReindexSendsHeaders.

public void testReindexSendsHeaders() throws Exception {
    ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest").setRemoteInfo(newRemoteInfo(null, null, singletonMap(TestFilter.EXAMPLE_HEADER, "doesn't matter")));
    ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get());
    assertEquals(RestStatus.BAD_REQUEST, e.status());
    assertThat(e.getMessage(), containsString("Hurray! Sent the header!"));
}
Also used : ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 8 with ElasticsearchStatusException

use of org.elasticsearch.ElasticsearchStatusException in project elasticsearch by elastic.

the class ReindexFromRemoteWithAuthTests method testReindexWithoutAuthenticationWhenRequired.

public void testReindexWithoutAuthenticationWhenRequired() throws Exception {
    ReindexRequestBuilder request = ReindexAction.INSTANCE.newRequestBuilder(client()).source("source").destination("dest").setRemoteInfo(newRemoteInfo(null, null, emptyMap()));
    ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> request.get());
    assertEquals(RestStatus.UNAUTHORIZED, e.status());
    assertThat(e.getMessage(), containsString("\"reason\":\"Authentication required\""));
    assertThat(e.getMessage(), containsString("\"WWW-Authenticate\":\"Basic realm=auth-realm\""));
}
Also used : ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Aggregations

ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)8 IOException (java.io.IOException)4 ElasticsearchException (org.elasticsearch.ElasticsearchException)3 RestStatus (org.elasticsearch.rest.RestStatus)3 HttpEntity (org.apache.http.HttpEntity)2 ParsingException (org.elasticsearch.common.ParsingException)2 XContentParser (org.elasticsearch.common.xcontent.XContentParser)2 FileNotFoundException (java.io.FileNotFoundException)1 ContentTooLongException (org.apache.http.ContentTooLongException)1 StringEntity (org.apache.http.entity.StringEntity)1 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)1 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)1 ActionRequestValidationException (org.elasticsearch.action.ActionRequestValidationException)1 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)1 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)1 GetResponse (org.elasticsearch.action.get.GetResponse)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 MainResponse (org.elasticsearch.action.main.MainResponse)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)1