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());
}
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");
}
}
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]");
}
}
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());
}
}
Aggregations