Search in sources :

Example 1 with OpenSearchException

use of org.opensearch.client.opensearch._types.OpenSearchException in project opensearch-java by opensearch-project.

the class RequestTest method errorResponse.

@Test
public void errorResponse() throws Exception {
    BooleanResponse exists = highLevelClient().exists(_0 -> _0.index("doesnotexist").id("reallynot"));
    assertFalse(exists.value());
    OpenSearchException ex = assertThrows(OpenSearchException.class, () -> {
        GetResponse<String> response = highLevelClient().get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class);
    });
    assertEquals(404, ex.status());
    assertEquals("index_not_found_exception", ex.error().type());
    assertEquals("doesnotexist", ex.error().metadata().get("index").to(String.class));
    ExecutionException ee = assertThrows(ExecutionException.class, () -> {
        OpenSearchAsyncClient aClient = new OpenSearchAsyncClient(highLevelClient()._transport());
        GetResponse<String> response = aClient.get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class).get();
    });
    ex = ((OpenSearchException) ee.getCause());
    assertEquals(404, ex.status());
    assertEquals("index_not_found_exception", ex.error().type());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) HistogramAggregate(org.opensearch.client.opensearch._types.aggregations.HistogramAggregate) OperationType(org.opensearch.client.opensearch.core.bulk.OperationType) Property(org.opensearch.client.opensearch._types.mapping.Property) IndexResponse(org.opensearch.client.opensearch.core.IndexResponse) Map(java.util.Map) Refresh(org.opensearch.client.opensearch._types.Refresh) GetIndexResponse(org.opensearch.client.opensearch.indices.GetIndexResponse) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) RequestItem(org.opensearch.client.opensearch.core.msearch.RequestItem) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IndexState(org.opensearch.client.opensearch.indices.IndexState) Test(org.junit.Test) IOException(java.io.IOException) BulkResponse(org.opensearch.client.opensearch.core.BulkResponse) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) ClearScrollResponse(org.opensearch.client.opensearch.core.ClearScrollResponse) MsearchResponse(org.opensearch.client.opensearch.core.MsearchResponse) GetIndicesSettingsResponse(org.opensearch.client.opensearch.indices.GetIndicesSettingsResponse) ModelTestCase(org.opensearch.client.opensearch.model.ModelTestCase) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) NodesResponse(org.opensearch.client.opensearch.cat.NodesResponse) GetResponse(org.opensearch.client.opensearch.core.GetResponse) Collections(java.util.Collections) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) ExecutionException(java.util.concurrent.ExecutionException) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) Test(org.junit.Test)

Example 2 with OpenSearchException

use of org.opensearch.client.opensearch._types.OpenSearchException in project opensearch-java by opensearch-project.

the class ClusterClientIT method testClusterUpdateSettingNonExistent.

public void testClusterUpdateSettingNonExistent() throws IOException {
    OpenSearchClient openSearchClient = highLevelClient();
    String setting = "no_idea_what_you_are_talking_about";
    int value = 10;
    Map<String, JsonData> transientSettingsMap = new HashMap<>();
    transientSettingsMap.put(setting, JsonData.of(value));
    PutClusterSettingsRequest request = new PutClusterSettingsRequest.Builder().transient_(transientSettingsMap).build();
    try {
        openSearchClient.cluster().putSettings(request);
        fail();
    } catch (OpenSearchException e) {
        assertNotNull(e);
        assertEquals(e.response().status(), 400);
        assertEquals(e.getMessage(), "Request failed: [illegal_argument_exception] " + "transient setting [no_idea_what_you_are_talking_about], not recognized");
    }
}
Also used : HashMap(java.util.HashMap) PutClusterSettingsRequest(org.opensearch.client.opensearch.cluster.PutClusterSettingsRequest) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) JsonData(org.opensearch.client.json.JsonData)

Example 3 with OpenSearchException

use of org.opensearch.client.opensearch._types.OpenSearchException in project opensearch-java by opensearch-project.

the class IndicesClientIT method testGetSettingsNonExistentIndex.

public void testGetSettingsNonExistentIndex() throws IOException {
    String nonExistentIndex = "index_that_doesnt_exist";
    GetIndicesSettingsRequest getIndicesSettingsRequest = new GetIndicesSettingsRequest.Builder().index(nonExistentIndex).build();
    try {
        highLevelClient().indices().getSettings(getIndicesSettingsRequest);
        fail();
    } catch (OpenSearchException ex) {
        assertNotNull(ex);
        assertEquals(ex.status(), 404);
        assertEquals(ex.getMessage(), "Request failed: [index_not_found_exception] " + "no such index [index_that_doesnt_exist]");
    }
}
Also used : GetIndicesSettingsRequest(org.opensearch.client.opensearch.indices.GetIndicesSettingsRequest) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException)

Example 4 with OpenSearchException

use of org.opensearch.client.opensearch._types.OpenSearchException in project opensearch-java by opensearch-project.

the class RestClientTransport method getHighLevelResponse.

private <ResponseT, ErrorT> ResponseT getHighLevelResponse(org.opensearch.client.Response clientResp, Endpoint<?, ResponseT, ErrorT> endpoint) throws IOException {
    try {
        int statusCode = clientResp.getStatusLine().getStatusCode();
        if (endpoint.isError(statusCode)) {
            JsonpDeserializer<ErrorT> errorDeserializer = endpoint.errorDeserializer(statusCode);
            if (errorDeserializer == null) {
                throw new TransportException("Request failed with status code '" + statusCode + "'", new ResponseException(clientResp));
            }
            HttpEntity entity = clientResp.getEntity();
            if (entity == null) {
                throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp));
            }
            // We may have to replay it.
            entity = new BufferedHttpEntity(entity);
            try {
                InputStream content = entity.getContent();
                try (JsonParser parser = mapper.jsonProvider().createParser(content)) {
                    ErrorT error = errorDeserializer.deserialize(parser, mapper);
                    // TODO: have the endpoint provide the exception constructor
                    throw new OpenSearchException((ErrorResponse) error);
                }
            } catch (MissingRequiredPropertyException errorEx) {
                // Could not decode exception, try the response type
                try {
                    ResponseT response = decodeResponse(statusCode, entity, clientResp, endpoint);
                    return response;
                } catch (Exception respEx) {
                    // No better luck: throw the original error decoding exception
                    throw new TransportException("Failed to decode error response", new ResponseException(clientResp));
                }
            }
        } else {
            return decodeResponse(statusCode, clientResp.getEntity(), clientResp, endpoint);
        }
    } finally {
        EntityUtils.consume(clientResp.getEntity());
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) ResponseException(org.opensearch.client.ResponseException) InputStream(java.io.InputStream) TransportException(org.opensearch.client.transport.TransportException) BooleanEndpoint(org.opensearch.client.transport.endpoints.BooleanEndpoint) Endpoint(org.opensearch.client.transport.Endpoint) JsonEndpoint(org.opensearch.client.transport.JsonEndpoint) TransportException(org.opensearch.client.transport.TransportException) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IOException(java.io.IOException) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) ResponseException(org.opensearch.client.ResponseException) BufferedHttpEntity(org.apache.http.entity.BufferedHttpEntity) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) MissingRequiredPropertyException(org.opensearch.client.util.MissingRequiredPropertyException) JsonParser(jakarta.json.stream.JsonParser)

Aggregations

OpenSearchException (org.opensearch.client.opensearch._types.OpenSearchException)4 IOException (java.io.IOException)2 JsonParser (jakarta.json.stream.JsonParser)1 InputStream (java.io.InputStream)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 HttpEntity (org.apache.http.HttpEntity)1 BufferedHttpEntity (org.apache.http.entity.BufferedHttpEntity)1 Test (org.junit.Test)1 ResponseException (org.opensearch.client.ResponseException)1 JsonData (org.opensearch.client.json.JsonData)1 OpenSearchAsyncClient (org.opensearch.client.opensearch.OpenSearchAsyncClient)1 OpenSearchClient (org.opensearch.client.opensearch.OpenSearchClient)1 Refresh (org.opensearch.client.opensearch._types.Refresh)1 HistogramAggregate (org.opensearch.client.opensearch._types.aggregations.HistogramAggregate)1 Property (org.opensearch.client.opensearch._types.mapping.Property)1