Skip to content

Commit

Permalink
Improve IndexNotFoundException's default error message (#34649)
Browse files Browse the repository at this point in the history
This commit adds the index name to the error message when an index is not found.
  • Loading branch information
atapin authored and kcm committed Oct 30, 2018
1 parent d44cc7e commit 0c50308
Show file tree
Hide file tree
Showing 30 changed files with 94 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void testGet() throws IOException {
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
() -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]", exception.getMessage());
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]", exception.getMessage());
assertEquals("index", exception.getMetadata("es.index").get(0));
}
IndexRequest index = new IndexRequest("index", "type", "id");
Expand Down Expand Up @@ -288,15 +288,15 @@ public void testMultiGet() throws IOException {
assertEquals("id1", response.getResponses()[0].getFailure().getId());
assertEquals("type", response.getResponses()[0].getFailure().getType());
assertEquals("index", response.getResponses()[0].getFailure().getIndex());
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]",
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]",
response.getResponses()[0].getFailure().getFailure().getMessage());

assertTrue(response.getResponses()[1].isFailed());
assertNull(response.getResponses()[1].getResponse());
assertEquals("id2", response.getResponses()[1].getId());
assertEquals("type", response.getResponses()[1].getType());
assertEquals("index", response.getResponses()[1].getIndex());
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]",
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]",
response.getResponses()[1].getFailure().getFailure().getMessage());
}
BulkRequest bulk = new BulkRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ public void testFromXContentWithElasticsearchException() throws IOException {
" \"root_cause\": [" +
" {" +
" \"type\": \"index_not_found_exception\"," +
" \"reason\": \"no such index\"," +
" \"reason\": \"no such index [index]\"," +
" \"resource.type\": \"index_or_alias\"," +
" \"resource.id\": \"index\"," +
" \"index_uuid\": \"_na_\"," +
" \"index\": \"index\"" +
" }" +
" ]," +
" \"type\": \"index_not_found_exception\"," +
" \"reason\": \"no such index\"," +
" \"reason\": \"no such index [index]\"," +
" \"resource.type\": \"index_or_alias\"," +
" \"resource.id\": \"index\"," +
" \"index_uuid\": \"_na_\"," +
Expand All @@ -131,7 +131,7 @@ public void testFromXContentWithElasticsearchException() throws IOException {
assertThat(getAliasesResponse.getError(), nullValue());
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getException().getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,8 @@ public void testAliasesNonExistentIndex() throws IOException {
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(nonExistentIndexRequest,
highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync));
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(exception.getMessage(), equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
assertThat(exception.getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
assertThat(exception.getMetadata("es.index"), hasItem(nonExistentIndex));

createIndex(index, Settings.EMPTY);
Expand All @@ -583,7 +584,8 @@ public void testAliasesNonExistentIndex() throws IOException {
exception = expectThrows(ElasticsearchStatusException.class,
() -> execute(mixedRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync));
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(exception.getMessage(), equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
assertThat(exception.getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
assertThat(exception.getMetadata("es.index"), hasItem(nonExistentIndex));
assertThat(exception.getMetadata("es.index"), not(hasItem(index)));
assertThat(aliasExists(index, alias), equalTo(false));
Expand All @@ -595,7 +597,8 @@ public void testAliasesNonExistentIndex() throws IOException {
exception = expectThrows(ElasticsearchException.class, () -> execute(removeIndexRequest, highLevelClient().indices()::updateAliases,
highLevelClient().indices()::updateAliasesAsync));
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(exception.getMessage(), equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
assertThat(exception.getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
assertThat(exception.getMetadata("es.index"), hasItem(nonExistentIndex));
assertThat(exception.getMetadata("es.index"), not(hasItem(index)));
assertThat(aliasExists(index, alias), equalTo(false));
Expand Down Expand Up @@ -1060,7 +1063,7 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getException().getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]"));
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest(alias);
Expand All @@ -1077,15 +1080,15 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getException().getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index").aliases(alias);
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias,
highLevelClient().indices()::getAliasAsync);
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(getAliasesResponse.getException().getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
}
{
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices("non_existent_index*");
Expand Down Expand Up @@ -1199,7 +1202,8 @@ public void testIndexPutSettingNonExistent() throws IOException {
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(indexUpdateSettingsRequest,
highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync));
assertEquals(RestStatus.NOT_FOUND, exception.status());
assertThat(exception.getMessage(), equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
assertThat(exception.getMessage(),
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]"));

createIndex(index, Settings.EMPTY);
exception = expectThrows(ElasticsearchException.class, () -> execute(indexUpdateSettingsRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ public void testExplainNonExistent() throws IOException {
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
assertThat(exception.getIndex().getName(), equalTo("non_existent_index"));
assertThat(exception.getDetailedMessage(),
containsString("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
containsString("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
}
{
ExplainRequest explainRequest = new ExplainRequest("index1", "doc", "999");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,7 @@ public void testMultiGet() throws Exception {
// TODO status is broken! fix in a followup
// assertEquals(RestStatus.NOT_FOUND, ee.status()); // <4>
assertThat(e.getMessage(),
containsString("reason=no such index")); // <5>
containsString("reason=no such index [missing_index]")); // <5>
// end::multi-get-indexnotfound

// tag::multi-get-execute-listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void testBasic() throws Exception {
MultiSearchTemplateResponse.Item response4 = response.getResponses()[3];
assertThat(response4.isFailure(), is(true));
assertThat(response4.getFailure(), instanceOf(IndexNotFoundException.class));
assertThat(response4.getFailure().getMessage(), equalTo("no such index"));
assertThat(response4.getFailure().getMessage(), equalTo("no such index [unknown]"));

MultiSearchTemplateResponse.Item response5 = response.getResponses()[4];
assertThat(response5.isFailure(), is(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void testResponseOnSearchFailure() throws Exception {
assertThat(e.getMessage(),
either(containsString("all shards failed"))
.or(containsString("No search context found"))
.or(containsString("no such index"))
.or(containsString("no such index [source]"))
);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ teardown:
transient:
action.auto_create_index: false
- do:
catch: /no such index and \[action.auto_create_index\] is \[false\]/
catch: /no such index \[dest\] and \[action.auto_create_index\] is \[false\]/
reindex:
body:
source:
Expand All @@ -41,7 +41,7 @@ teardown:
id: 1
body: { "text": "test" }
- do:
catch: /no such index and \[action.auto_create_index\] \(\[test\]\) doesn't match/
catch: /no such index \[dest\] and \[action.auto_create_index\] \(\[test\]\) doesn't match/
reindex:
body:
source:
Expand All @@ -63,7 +63,7 @@ teardown:
id: 1
body: { "text": "test" }
- do:
catch: /no such index and \[action.auto_create_index\] contains \[-dest\] which forbids automatic creation of the index/
catch: /no such index \[dest\] and \[action.auto_create_index\] contains \[-dest\] which forbids automatic creation of the index/
reindex:
body:
source:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public boolean shouldAutoCreate(String index, ClusterState state) {
// One volatile read, so that all checks are done against the same instance:
final AutoCreate autoCreate = this.autoCreate;
if (autoCreate.autoCreateIndex == false) {
throw new IndexNotFoundException("no such index and [" + AUTO_CREATE_INDEX_SETTING.getKey() + "] is [false]", index);
throw new IndexNotFoundException("[" + AUTO_CREATE_INDEX_SETTING.getKey() + "] is [false]", index);
}
if (dynamicMappingDisabled) {
throw new IndexNotFoundException("no such index and [" + MapperService.INDEX_MAPPER_DYNAMIC_SETTING.getKey() + "] is [false]",
throw new IndexNotFoundException("[" + MapperService.INDEX_MAPPER_DYNAMIC_SETTING.getKey() + "] is [false]",
index);
}
// matches not set, default value of "true"
Expand All @@ -90,11 +90,11 @@ public boolean shouldAutoCreate(String index, ClusterState state) {
if (include) {
return true;
}
throw new IndexNotFoundException("no such index and [" + AUTO_CREATE_INDEX_SETTING.getKey() + "] contains [-"
throw new IndexNotFoundException("[" + AUTO_CREATE_INDEX_SETTING.getKey() + "] contains [-"
+ indexExpression + "] which forbids automatic creation of the index", index);
}
}
throw new IndexNotFoundException("no such index and [" + AUTO_CREATE_INDEX_SETTING.getKey() + "] ([" + autoCreate
throw new IndexNotFoundException("[" + AUTO_CREATE_INDEX_SETTING.getKey() + "] ([" + autoCreate
+ "]) doesn't match", index);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class IndexNotFoundException extends ResourceNotFoundException {
* Construct with a custom message.
*/
public IndexNotFoundException(String message, String index) {
super(message);
super("no such index [" + index + "] and " + message);
setIndex(index);
}

Expand All @@ -37,7 +37,7 @@ public IndexNotFoundException(String index) {
}

public IndexNotFoundException(String index, Throwable cause) {
super("no such index", cause);
super("no such index [" + index + "]", cause);
setIndex(index);
}

Expand All @@ -46,7 +46,7 @@ public IndexNotFoundException(Index index) {
}

public IndexNotFoundException(Index index, Throwable cause) {
super("no such index", cause);
super("no such index [" + index.getName() + "]", cause);
setIndex(index);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void testGuessRootCause() {
ElasticsearchException[] rootCauses = exception.guessRootCauses();
assertEquals(rootCauses.length, 1);
assertEquals(ElasticsearchException.getExceptionName(rootCauses[0]), "index_not_found_exception");
assertEquals(rootCauses[0].getMessage(), "no such index");
assertEquals("no such index [foo]", rootCauses[0].getMessage());
ShardSearchFailure failure = new ShardSearchFailure(new ParsingException(1, 2, "foobar", null),
new SearchShardTarget("node_1", new Index("foo", "_na_"), 1, null));
ShardSearchFailure failure1 = new ShardSearchFailure(new ParsingException(1, 2, "foobar", null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testSimpleUnknownIndex() {
client().admin().indices().prepareGetIndex().addIndices("missing_idx").get();
fail("Expected IndexNotFoundException");
} catch (IndexNotFoundException e) {
assertThat(e.getMessage(), is("no such index"));
assertThat(e.getMessage(), is("no such index [missing_idx]"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testBulkProcessorAutoCreateRestrictions() throws Exception {
assertEquals(3, responses.length);
assertFalse("Operation on existing index should succeed", responses[0].isFailed());
assertTrue("Missing index should have been flagged", responses[1].isFailed());
assertEquals("[wontwork] IndexNotFoundException[no such index]", responses[1].getFailureMessage());
assertEquals("[wontwork] IndexNotFoundException[no such index [wontwork]]", responses[1].getFailureMessage());
assertFalse("Operation on existing index should succeed", responses[2].isFailed());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ public void testHandleSpaces() { // see #21449
public void testAutoCreationDisabled() {
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), false).build();
AutoCreateIndex autoCreateIndex = newAutoCreateIndex(settings);
String randomIndex = randomAlphaOfLengthBetween(1, 10);
IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () ->
autoCreateIndex.shouldAutoCreate(randomAlphaOfLengthBetween(1, 10), buildClusterState()));
assertEquals("no such index and [action.auto_create_index] is [false]", e.getMessage());
autoCreateIndex.shouldAutoCreate(randomIndex, buildClusterState()));
assertEquals("no such index [" + randomIndex + "] and [action.auto_create_index] is [false]", e.getMessage());
}

public void testAutoCreationEnabled() {
Expand Down Expand Up @@ -207,14 +208,15 @@ private AutoCreateIndex newAutoCreateIndex(Settings settings) {
private void expectNotMatch(ClusterState clusterState, AutoCreateIndex autoCreateIndex, String index) {
IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () ->
autoCreateIndex.shouldAutoCreate(index, clusterState));
assertEquals("no such index and [action.auto_create_index] ([" + autoCreateIndex.getAutoCreate() + "]) doesn't match",
e.getMessage());
assertEquals(
"no such index [" + index + "] and [action.auto_create_index] ([" + autoCreateIndex.getAutoCreate() + "]) doesn't match",
e.getMessage());
}

private void expectForbidden(ClusterState clusterState, AutoCreateIndex autoCreateIndex, String index, String forbiddingPattern) {
IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () ->
autoCreateIndex.shouldAutoCreate(index, clusterState));
assertEquals("no such index and [action.auto_create_index] contains [" + forbiddingPattern
assertEquals("no such index [" + index + "] and [action.auto_create_index] contains [" + forbiddingPattern
+ "] which forbids automatic creation of the index", e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void testMissingIndexThrowsMissingIndex() throws Exception {
MultiTermVectorsResponse response = mtvBuilder.execute().actionGet();
assertThat(response.getResponses().length, equalTo(1));
assertThat(response.getResponses()[0].getFailure().getCause(), instanceOf(IndexNotFoundException.class));
assertThat(response.getResponses()[0].getFailure().getCause().getMessage(), equalTo("no such index"));
assertThat(response.getResponses()[0].getFailure().getCause().getMessage(), equalTo("no such index [testX]"));
}

public void testMultiTermVectorsWithVersion() throws Exception {
Expand Down
Loading

0 comments on commit 0c50308

Please sign in to comment.