Skip to content

Commit

Permalink
[Rename] ElasticsearchStatusException in server module (#172)
Browse files Browse the repository at this point in the history
This commit refactors the ElasticsearchStatusException in the server module to
OpenSearchStatusException. References and usages throughout the rest of the
codebase are fully refactored.

Signed-off-by: Nicholas Knize <nknize@amazon.com>
  • Loading branch information
nknize committed Mar 22, 2021
1 parent 1015c8e commit 64d7e7e
Show file tree
Hide file tree
Showing 19 changed files with 69 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import org.apache.http.HttpEntity;
import org.elasticsearch.OpenSearchException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.OpenSearchStatusException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
Expand Down Expand Up @@ -1688,29 +1688,29 @@ public void onFailure(Exception exception) {
/**
* Converts a {@link ResponseException} obtained from the low level REST client into an {@link OpenSearchException}.
* 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}
* If no response body was returned or anything goes wrong while parsing the error, returns a new {@link OpenSearchStatusException}
* 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) {
protected final OpenSearchStatusException parseResponseException(ResponseException responseException) {
Response response = responseException.getResponse();
HttpEntity entity = response.getEntity();
ElasticsearchStatusException elasticsearchException;
OpenSearchStatusException opensearchException;
RestStatus restStatus = RestStatus.fromCode(response.getStatusLine().getStatusCode());

if (entity == null) {
elasticsearchException = new ElasticsearchStatusException(
opensearchException = new OpenSearchStatusException(
responseException.getMessage(), restStatus, responseException);
} else {
try {
elasticsearchException = parseEntity(entity, BytesRestResponse::errorFromXContent);
elasticsearchException.addSuppressed(responseException);
opensearchException = parseEntity(entity, BytesRestResponse::errorFromXContent);
opensearchException.addSuppressed(responseException);
} catch (Exception e) {
elasticsearchException = new ElasticsearchStatusException("Unable to parse response body", restStatus, responseException);
elasticsearchException.addSuppressed(e);
opensearchException = new OpenSearchStatusException("Unable to parse response body", restStatus, responseException);
opensearchException.addSuppressed(e);
}
}
return elasticsearchException;
return opensearchException;
}

protected final <Resp> Resp parseEntity(final HttpEntity entity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import org.apache.http.util.EntityUtils;
import org.elasticsearch.OpenSearchException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.OpenSearchStatusException;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest;
Expand Down Expand Up @@ -387,7 +387,7 @@ public void testComponentTemplates() throws Exception {
highLevelClient().cluster()::deleteComponentTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));

ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class,
OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class,
() -> execute(getComponentTemplatesRequest,
highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package org.elasticsearch.client;

import org.elasticsearch.OpenSearchException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.OpenSearchStatusException;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testDelete() throws IOException {
highLevelClient().index(
new IndexRequest("index").id(docId).source(Collections.singletonMap("foo", "bar"))
.versionType(VersionType.EXTERNAL).version(12), RequestOptions.DEFAULT);
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> {
DeleteRequest deleteRequest = new DeleteRequest("index", docId).versionType(VersionType.EXTERNAL).version(10);
execute(deleteRequest, highLevelClient()::delete, highLevelClient()::deleteAsync);
});
Expand Down Expand Up @@ -594,7 +594,7 @@ public void testIndex() throws IOException {
assertEquals("id", indexResponse.getId());
assertEquals(2L, indexResponse.getVersion());

ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> {
IndexRequest wrongRequest = new IndexRequest("index").id("id");
wrongRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("field", "test").endObject());
wrongRequest.setIfSeqNo(1L).setIfPrimaryTerm(5L);
Expand All @@ -608,7 +608,7 @@ public void testIndex() throws IOException {
assertEquals("index", exception.getMetadata("es.index").get(0));
}
{
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> {
IndexRequest indexRequest = new IndexRequest("index").id("missing_pipeline");
indexRequest.source(XContentBuilder.builder(xContentType.xContent()).startObject().field("field", "test").endObject());
indexRequest.setPipeline("missing");
Expand Down Expand Up @@ -644,7 +644,7 @@ public void testIndex() throws IOException {
assertEquals("_doc", indexResponse.getType());
assertEquals("with_create_op_type", indexResponse.getId());

ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> {
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> {
execute(indexRequest, highLevelClient()::index, highLevelClient()::indexAsync);
});

Expand Down Expand Up @@ -675,7 +675,7 @@ public void testUpdate() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("index", "does_not_exist");
updateRequest.doc(singletonMap("field", "value"), randomFrom(XContentType.values()));

ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () ->
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () ->
execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
assertEquals("Elasticsearch exception [type=document_missing_exception, reason=[_doc][does_not_exist]: document missing]",
Expand Down Expand Up @@ -712,7 +712,7 @@ public void testUpdate() throws IOException {
updateRequest.setIfSeqNo(lastUpdateSeqNo + (randomBoolean() ? 0 : 1));
updateRequest.setIfPrimaryTerm(lastUpdatePrimaryTerm + 1);
}
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () ->
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () ->
execute(updateRequest, highLevelClient()::update, highLevelClient()::updateAsync));
assertEquals(exception.toString(),RestStatus.CONFLICT, exception.status());
assertThat(exception.getMessage(), containsString("Elasticsearch exception [type=version_conflict_engine_exception"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.OpenSearchException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.OpenSearchStatusException;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
Expand Down Expand Up @@ -826,7 +826,7 @@ public void testAliasesNonExistentIndex() throws IOException {
IndicesAliasesRequest mixedRequest = new IndicesAliasesRequest();
mixedRequest.addAliasAction(new AliasActions(AliasActions.Type.ADD).indices(index).aliases(alias));
mixedRequest.addAliasAction(new AliasActions(AliasActions.Type.REMOVE).indices(nonExistentIndex).alias(alias));
exception = expectThrows(ElasticsearchStatusException.class,
exception = expectThrows(OpenSearchStatusException.class,
() -> execute(mixedRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync));
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(exception.getMessage(),
Expand Down Expand Up @@ -1651,7 +1651,7 @@ public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception {
.alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));


ElasticsearchStatusException badMappingError = expectThrows(ElasticsearchStatusException.class,
OpenSearchStatusException badMappingError = expectThrows(OpenSearchStatusException.class,
() -> execute(putTemplateRequest,
highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync));
assertThat(badMappingError.getDetailedMessage(),
Expand Down Expand Up @@ -1719,7 +1719,7 @@ public void testPutTemplateBadRequests() throws Exception {
PutIndexTemplateRequest unknownSettingTemplate = new PutIndexTemplateRequest("t3")
.patterns(Collections.singletonList("any"))
.settings(Settings.builder().put("this-setting-does-not-exist", 100));
ElasticsearchStatusException unknownSettingError = expectThrows(ElasticsearchStatusException.class,
OpenSearchStatusException unknownSettingError = expectThrows(OpenSearchStatusException.class,
() -> execute(unknownSettingTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync));
assertThat(unknownSettingError.getDetailedMessage(), containsString("unknown setting [index.this-setting-does-not-exist]"));
}
Expand Down Expand Up @@ -2077,7 +2077,7 @@ public void testDataStreams() throws Exception {

getDataStreamRequest = new GetDataStreamRequest(dataStreamName);
GetDataStreamRequest finalGetDataStreamRequest = getDataStreamRequest;
ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> execute(finalGetDataStreamRequest,
OpenSearchStatusException e = expectThrows(OpenSearchStatusException.class, () -> execute(finalGetDataStreamRequest,
indices::getDataStream, indices::getDataStreamAsync));
assertThat(e.status(), equalTo(RestStatus.NOT_FOUND));
}
Expand Down Expand Up @@ -2117,7 +2117,7 @@ public void testIndexTemplates() throws Exception {
highLevelClient().indices()::deleteIndexTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));

ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class,
OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class,
() -> execute(getComposableIndexTemplateRequest,
highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.OpenSearchException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.OpenSearchStatusException;
import org.elasticsearch.action.explain.ExplainRequest;
import org.elasticsearch.action.explain.ExplainResponse;
import org.elasticsearch.action.fieldcaps.FieldCapabilities;
Expand Down Expand Up @@ -340,7 +340,7 @@ public void testSearchWithRangeAgg() throws IOException {
searchSourceBuilder.size(0);
searchRequest.source(searchSourceBuilder);

ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class,
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class,
() -> execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync));
assertEquals(RestStatus.BAD_REQUEST, exception.status());
}
Expand Down Expand Up @@ -722,7 +722,7 @@ public void testSearchScroll() throws Exception {
assertTrue(clearScrollResponse.isSucceeded());

SearchScrollRequest scrollRequest = new SearchScrollRequest(searchResponse.getScrollId()).scroll(TimeValue.timeValueMinutes(2));
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class, () -> execute(scrollRequest,
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class, () -> execute(scrollRequest,
highLevelClient()::scroll, highLevelClient()::scrollAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
assertThat(exception.getRootCause(), instanceOf(OpenSearchException.class));
Expand Down Expand Up @@ -954,7 +954,7 @@ public void testNonExistentSearchTemplate() {
searchTemplateRequest.setScript("non-existent");
searchTemplateRequest.setScriptParams(Collections.emptyMap());

ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class,
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class,
() -> execute(searchTemplateRequest,
highLevelClient()::searchTemplate,
highLevelClient()::searchTemplateAsync));
Expand Down Expand Up @@ -1073,7 +1073,7 @@ public void testMultiSearchTemplateAllBad() throws Exception {
multiSearchTemplateRequest.add(badRequest2);

// The whole HTTP request should fail if no nested search requests are valid
ElasticsearchStatusException exception = expectThrows(ElasticsearchStatusException.class,
OpenSearchStatusException exception = expectThrows(OpenSearchStatusException.class,
() -> execute(multiSearchTemplateRequest, highLevelClient()::msearchTemplate,
highLevelClient()::msearchTemplateAsync));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* under the License.
*/

import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.OpenSearchStatusException;
import org.elasticsearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest;
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptRequest;
import org.elasticsearch.action.admin.cluster.storedscripts.GetStoredScriptResponse;
Expand Down Expand Up @@ -76,7 +76,7 @@ public void testDeleteStoredScript() throws Exception {

GetStoredScriptRequest getRequest = new GetStoredScriptRequest(id);

final ElasticsearchStatusException statusException = expectThrows(ElasticsearchStatusException.class,
final OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class,
() -> execute(getRequest, highLevelClient()::getScript,
highLevelClient()::getScriptAsync));
assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package org.elasticsearch.client.indices;

import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.OpenSearchStatusException;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.support.master.ShardsAcknowledgedResponse;
import org.elasticsearch.client.AbstractResponseTestCase;
Expand Down Expand Up @@ -215,7 +215,7 @@ private org.elasticsearch.action.admin.indices.close.CloseIndexResponse.ShardRes
Exception exception = randomFrom(new IndexNotFoundException(indexName),
new ActionNotFoundTransportException("test"),
new IOException("boom", new NullPointerException()),
new ElasticsearchStatusException("something", RestStatus.TOO_MANY_REQUESTS));
new OpenSearchStatusException("something", RestStatus.TOO_MANY_REQUESTS));
return new org.elasticsearch.action.admin.indices.close.CloseIndexResponse.ShardResult.Failure(indexName, shard, exception, nodeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.apache.logging.log4j.util.Supplier;
import org.elasticsearch.OpenSearchException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.OpenSearchException;;
import org.elasticsearch.OpenSearchStatusException;
import org.elasticsearch.Version;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.search.SearchRequest;
Expand Down Expand Up @@ -230,17 +230,17 @@ public void onFailure(Exception e) {
* 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) {
static OpenSearchStatusException 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);
return new OpenSearchStatusException(messagePrefix + bodyMessage(entity), status, cause);
} catch (IOException ioe) {
ElasticsearchStatusException e = new ElasticsearchStatusException(messagePrefix + "Failed to extract body.", status, cause);
OpenSearchStatusException e = new OpenSearchStatusException(messagePrefix + "Failed to extract body.", status, cause);
e.addSuppressed(ioe);
return e;
}
Expand Down
Loading

0 comments on commit 64d7e7e

Please sign in to comment.