Skip to content

Commit 5f58818

Browse files
atapinrjernst
authored andcommitted
Improve IndexNotFoundException's default error message (#34649)
This commit adds the index name to the error message when an index is not found.
1 parent 04f3e67 commit 5f58818

File tree

30 files changed

+94
-81
lines changed

30 files changed

+94
-81
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/CrudIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void testGet() throws IOException {
201201
ElasticsearchException exception = expectThrows(ElasticsearchException.class,
202202
() -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
203203
assertEquals(RestStatus.NOT_FOUND, exception.status());
204-
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]", exception.getMessage());
204+
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]", exception.getMessage());
205205
assertEquals("index", exception.getMetadata("es.index").get(0));
206206
}
207207
IndexRequest index = new IndexRequest("index", "type", "id");
@@ -288,15 +288,15 @@ public void testMultiGet() throws IOException {
288288
assertEquals("id1", response.getResponses()[0].getFailure().getId());
289289
assertEquals("type", response.getResponses()[0].getFailure().getType());
290290
assertEquals("index", response.getResponses()[0].getFailure().getIndex());
291-
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]",
291+
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]",
292292
response.getResponses()[0].getFailure().getFailure().getMessage());
293293

294294
assertTrue(response.getResponses()[1].isFailed());
295295
assertNull(response.getResponses()[1].getResponse());
296296
assertEquals("id2", response.getResponses()[1].getId());
297297
assertEquals("type", response.getResponses()[1].getType());
298298
assertEquals("index", response.getResponses()[1].getIndex());
299-
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index]",
299+
assertEquals("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]",
300300
response.getResponses()[1].getFailure().getFailure().getMessage());
301301
}
302302
BulkRequest bulk = new BulkRequest();

client/rest-high-level/src/test/java/org/elasticsearch/client/GetAliasesResponseTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ public void testFromXContentWithElasticsearchException() throws IOException {
109109
" \"root_cause\": [" +
110110
" {" +
111111
" \"type\": \"index_not_found_exception\"," +
112-
" \"reason\": \"no such index\"," +
112+
" \"reason\": \"no such index [index]\"," +
113113
" \"resource.type\": \"index_or_alias\"," +
114114
" \"resource.id\": \"index\"," +
115115
" \"index_uuid\": \"_na_\"," +
116116
" \"index\": \"index\"" +
117117
" }" +
118118
" ]," +
119119
" \"type\": \"index_not_found_exception\"," +
120-
" \"reason\": \"no such index\"," +
120+
" \"reason\": \"no such index [index]\"," +
121121
" \"resource.type\": \"index_or_alias\"," +
122122
" \"resource.id\": \"index\"," +
123123
" \"index_uuid\": \"_na_\"," +
@@ -131,7 +131,7 @@ public void testFromXContentWithElasticsearchException() throws IOException {
131131
assertThat(getAliasesResponse.getError(), nullValue());
132132
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
133133
assertThat(getAliasesResponse.getException().getMessage(),
134-
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
134+
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]"));
135135
}
136136
}
137137

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ public void testAliasesNonExistentIndex() throws IOException {
573573
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(nonExistentIndexRequest,
574574
highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync));
575575
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
576-
assertThat(exception.getMessage(), equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
576+
assertThat(exception.getMessage(),
577+
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
577578
assertThat(exception.getMetadata("es.index"), hasItem(nonExistentIndex));
578579

579580
createIndex(index, Settings.EMPTY);
@@ -583,7 +584,8 @@ public void testAliasesNonExistentIndex() throws IOException {
583584
exception = expectThrows(ElasticsearchStatusException.class,
584585
() -> execute(mixedRequest, highLevelClient().indices()::updateAliases, highLevelClient().indices()::updateAliasesAsync));
585586
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
586-
assertThat(exception.getMessage(), equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
587+
assertThat(exception.getMessage(),
588+
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
587589
assertThat(exception.getMetadata("es.index"), hasItem(nonExistentIndex));
588590
assertThat(exception.getMetadata("es.index"), not(hasItem(index)));
589591
assertThat(aliasExists(index, alias), equalTo(false));
@@ -595,7 +597,8 @@ public void testAliasesNonExistentIndex() throws IOException {
595597
exception = expectThrows(ElasticsearchException.class, () -> execute(removeIndexRequest, highLevelClient().indices()::updateAliases,
596598
highLevelClient().indices()::updateAliasesAsync));
597599
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
598-
assertThat(exception.getMessage(), equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
600+
assertThat(exception.getMessage(),
601+
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
599602
assertThat(exception.getMetadata("es.index"), hasItem(nonExistentIndex));
600603
assertThat(exception.getMetadata("es.index"), not(hasItem(index)));
601604
assertThat(aliasExists(index, alias), equalTo(false));
@@ -1060,7 +1063,7 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
10601063
highLevelClient().indices()::getAliasAsync);
10611064
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
10621065
assertThat(getAliasesResponse.getException().getMessage(),
1063-
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
1066+
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]"));
10641067
}
10651068
{
10661069
GetAliasesRequest getAliasesRequest = new GetAliasesRequest(alias);
@@ -1077,15 +1080,15 @@ public void testGetAliasesNonExistentIndexOrAlias() throws IOException {
10771080
highLevelClient().indices()::getAliasAsync);
10781081
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
10791082
assertThat(getAliasesResponse.getException().getMessage(),
1080-
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
1083+
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
10811084
}
10821085
{
10831086
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices(index, "non_existent_index").aliases(alias);
10841087
GetAliasesResponse getAliasesResponse = execute(getAliasesRequest, highLevelClient().indices()::getAlias,
10851088
highLevelClient().indices()::getAliasAsync);
10861089
assertThat(getAliasesResponse.status(), equalTo(RestStatus.NOT_FOUND));
10871090
assertThat(getAliasesResponse.getException().getMessage(),
1088-
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
1091+
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
10891092
}
10901093
{
10911094
GetAliasesRequest getAliasesRequest = new GetAliasesRequest().indices("non_existent_index*");
@@ -1199,7 +1202,8 @@ public void testIndexPutSettingNonExistent() throws IOException {
11991202
ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> execute(indexUpdateSettingsRequest,
12001203
highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync));
12011204
assertEquals(RestStatus.NOT_FOUND, exception.status());
1202-
assertThat(exception.getMessage(), equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
1205+
assertThat(exception.getMessage(),
1206+
equalTo("Elasticsearch exception [type=index_not_found_exception, reason=no such index [index]]"));
12031207

12041208
createIndex(index, Settings.EMPTY);
12051209
exception = expectThrows(ElasticsearchException.class, () -> execute(indexUpdateSettingsRequest,

client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ public void testExplainNonExistent() throws IOException {
10791079
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
10801080
assertThat(exception.getIndex().getName(), equalTo("non_existent_index"));
10811081
assertThat(exception.getDetailedMessage(),
1082-
containsString("Elasticsearch exception [type=index_not_found_exception, reason=no such index]"));
1082+
containsString("Elasticsearch exception [type=index_not_found_exception, reason=no such index [non_existent_index]]"));
10831083
}
10841084
{
10851085
ExplainRequest explainRequest = new ExplainRequest("index1", "doc", "999");

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/CRUDDocumentationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,7 @@ public void testMultiGet() throws Exception {
17151715
// TODO status is broken! fix in a followup
17161716
// assertEquals(RestStatus.NOT_FOUND, ee.status()); // <4>
17171717
assertThat(e.getMessage(),
1718-
containsString("reason=no such index")); // <5>
1718+
containsString("reason=no such index [missing_index]")); // <5>
17191719
// end::multi-get-indexnotfound
17201720

17211721
// tag::multi-get-execute-listener

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/MultiSearchTemplateIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void testBasic() throws Exception {
169169
MultiSearchTemplateResponse.Item response4 = response.getResponses()[3];
170170
assertThat(response4.isFailure(), is(true));
171171
assertThat(response4.getFailure(), instanceOf(IndexNotFoundException.class));
172-
assertThat(response4.getFailure().getMessage(), equalTo("no such index"));
172+
assertThat(response4.getFailure().getMessage(), equalTo("no such index [unknown]"));
173173

174174
MultiSearchTemplateResponse.Item response5 = response.getResponses()[4];
175175
assertThat(response5.isFailure(), is(false));

modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexFailureTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void testResponseOnSearchFailure() throws Exception {
120120
assertThat(e.getMessage(),
121121
either(containsString("all shards failed"))
122122
.or(containsString("No search context found"))
123-
.or(containsString("no such index"))
123+
.or(containsString("no such index [source]"))
124124
);
125125
return;
126126
}

modules/reindex/src/test/resources/rest-api-spec/test/reindex/25_no_auto_create.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ teardown:
1919
transient:
2020
action.auto_create_index: false
2121
- do:
22-
catch: /no such index and \[action.auto_create_index\] is \[false\]/
22+
catch: /no such index \[dest\] and \[action.auto_create_index\] is \[false\]/
2323
reindex:
2424
body:
2525
source:
@@ -41,7 +41,7 @@ teardown:
4141
id: 1
4242
body: { "text": "test" }
4343
- do:
44-
catch: /no such index and \[action.auto_create_index\] \(\[test\]\) doesn't match/
44+
catch: /no such index \[dest\] and \[action.auto_create_index\] \(\[test\]\) doesn't match/
4545
reindex:
4646
body:
4747
source:
@@ -63,7 +63,7 @@ teardown:
6363
id: 1
6464
body: { "text": "test" }
6565
- do:
66-
catch: /no such index and \[action.auto_create_index\] contains \[-dest\] which forbids automatic creation of the index/
66+
catch: /no such index \[dest\] and \[action.auto_create_index\] contains \[-dest\] which forbids automatic creation of the index/
6767
reindex:
6868
body:
6969
source:

server/src/main/java/org/elasticsearch/action/support/AutoCreateIndex.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ public boolean shouldAutoCreate(String index, ClusterState state) {
7373
// One volatile read, so that all checks are done against the same instance:
7474
final AutoCreate autoCreate = this.autoCreate;
7575
if (autoCreate.autoCreateIndex == false) {
76-
throw new IndexNotFoundException("no such index and [" + AUTO_CREATE_INDEX_SETTING.getKey() + "] is [false]", index);
76+
throw new IndexNotFoundException("[" + AUTO_CREATE_INDEX_SETTING.getKey() + "] is [false]", index);
7777
}
7878
if (dynamicMappingDisabled) {
79-
throw new IndexNotFoundException("no such index and [" + MapperService.INDEX_MAPPER_DYNAMIC_SETTING.getKey() + "] is [false]",
79+
throw new IndexNotFoundException("[" + MapperService.INDEX_MAPPER_DYNAMIC_SETTING.getKey() + "] is [false]",
8080
index);
8181
}
8282
// matches not set, default value of "true"
@@ -90,11 +90,11 @@ public boolean shouldAutoCreate(String index, ClusterState state) {
9090
if (include) {
9191
return true;
9292
}
93-
throw new IndexNotFoundException("no such index and [" + AUTO_CREATE_INDEX_SETTING.getKey() + "] contains [-"
93+
throw new IndexNotFoundException("[" + AUTO_CREATE_INDEX_SETTING.getKey() + "] contains [-"
9494
+ indexExpression + "] which forbids automatic creation of the index", index);
9595
}
9696
}
97-
throw new IndexNotFoundException("no such index and [" + AUTO_CREATE_INDEX_SETTING.getKey() + "] ([" + autoCreate
97+
throw new IndexNotFoundException("[" + AUTO_CREATE_INDEX_SETTING.getKey() + "] ([" + autoCreate
9898
+ "]) doesn't match", index);
9999
}
100100

server/src/main/java/org/elasticsearch/index/IndexNotFoundException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public final class IndexNotFoundException extends ResourceNotFoundException {
2828
* Construct with a custom message.
2929
*/
3030
public IndexNotFoundException(String message, String index) {
31-
super(message);
31+
super("no such index [" + index + "] and " + message);
3232
setIndex(index);
3333
}
3434

@@ -37,7 +37,7 @@ public IndexNotFoundException(String index) {
3737
}
3838

3939
public IndexNotFoundException(String index, Throwable cause) {
40-
super("no such index", cause);
40+
super("no such index [" + index + "]", cause);
4141
setIndex(index);
4242
}
4343

@@ -46,7 +46,7 @@ public IndexNotFoundException(Index index) {
4646
}
4747

4848
public IndexNotFoundException(Index index, Throwable cause) {
49-
super("no such index", cause);
49+
super("no such index [" + index.getName() + "]", cause);
5050
setIndex(index);
5151
}
5252

server/src/test/java/org/elasticsearch/ElasticsearchExceptionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void testGuessRootCause() {
114114
ElasticsearchException[] rootCauses = exception.guessRootCauses();
115115
assertEquals(rootCauses.length, 1);
116116
assertEquals(ElasticsearchException.getExceptionName(rootCauses[0]), "index_not_found_exception");
117-
assertEquals(rootCauses[0].getMessage(), "no such index");
117+
assertEquals("no such index [foo]", rootCauses[0].getMessage());
118118
ShardSearchFailure failure = new ShardSearchFailure(new ParsingException(1, 2, "foobar", null),
119119
new SearchShardTarget("node_1", new Index("foo", "_na_"), 1, null));
120120
ShardSearchFailure failure1 = new ShardSearchFailure(new ParsingException(1, 2, "foobar", null),

server/src/test/java/org/elasticsearch/action/admin/indices/get/GetIndexIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void testSimpleUnknownIndex() {
7373
client().admin().indices().prepareGetIndex().addIndices("missing_idx").get();
7474
fail("Expected IndexNotFoundException");
7575
} catch (IndexNotFoundException e) {
76-
assertThat(e.getMessage(), is("no such index"));
76+
assertThat(e.getMessage(), is("no such index [missing_idx]"));
7777
}
7878
}
7979

server/src/test/java/org/elasticsearch/action/bulk/BulkProcessorClusterSettingsIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void testBulkProcessorAutoCreateRestrictions() throws Exception {
4545
assertEquals(3, responses.length);
4646
assertFalse("Operation on existing index should succeed", responses[0].isFailed());
4747
assertTrue("Missing index should have been flagged", responses[1].isFailed());
48-
assertEquals("[wontwork] IndexNotFoundException[no such index]", responses[1].getFailureMessage());
48+
assertEquals("[wontwork] IndexNotFoundException[no such index [wontwork]]", responses[1].getFailureMessage());
4949
assertFalse("Operation on existing index should succeed", responses[2].isFailed());
5050
}
5151
}

server/src/test/java/org/elasticsearch/action/support/AutoCreateIndexTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ public void testHandleSpaces() { // see #21449
8383
public void testAutoCreationDisabled() {
8484
Settings settings = Settings.builder().put(AutoCreateIndex.AUTO_CREATE_INDEX_SETTING.getKey(), false).build();
8585
AutoCreateIndex autoCreateIndex = newAutoCreateIndex(settings);
86+
String randomIndex = randomAlphaOfLengthBetween(1, 10);
8687
IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () ->
87-
autoCreateIndex.shouldAutoCreate(randomAlphaOfLengthBetween(1, 10), buildClusterState()));
88-
assertEquals("no such index and [action.auto_create_index] is [false]", e.getMessage());
88+
autoCreateIndex.shouldAutoCreate(randomIndex, buildClusterState()));
89+
assertEquals("no such index [" + randomIndex + "] and [action.auto_create_index] is [false]", e.getMessage());
8990
}
9091

9192
public void testAutoCreationEnabled() {
@@ -207,14 +208,15 @@ private AutoCreateIndex newAutoCreateIndex(Settings settings) {
207208
private void expectNotMatch(ClusterState clusterState, AutoCreateIndex autoCreateIndex, String index) {
208209
IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () ->
209210
autoCreateIndex.shouldAutoCreate(index, clusterState));
210-
assertEquals("no such index and [action.auto_create_index] ([" + autoCreateIndex.getAutoCreate() + "]) doesn't match",
211-
e.getMessage());
211+
assertEquals(
212+
"no such index [" + index + "] and [action.auto_create_index] ([" + autoCreateIndex.getAutoCreate() + "]) doesn't match",
213+
e.getMessage());
212214
}
213215

214216
private void expectForbidden(ClusterState clusterState, AutoCreateIndex autoCreateIndex, String index, String forbiddingPattern) {
215217
IndexNotFoundException e = expectThrows(IndexNotFoundException.class, () ->
216218
autoCreateIndex.shouldAutoCreate(index, clusterState));
217-
assertEquals("no such index and [action.auto_create_index] contains [" + forbiddingPattern
219+
assertEquals("no such index [" + index + "] and [action.auto_create_index] contains [" + forbiddingPattern
218220
+ "] which forbids automatic creation of the index", e.getMessage());
219221
}
220222
}

server/src/test/java/org/elasticsearch/action/termvectors/MultiTermVectorsIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void testMissingIndexThrowsMissingIndex() throws Exception {
7676
MultiTermVectorsResponse response = mtvBuilder.execute().actionGet();
7777
assertThat(response.getResponses().length, equalTo(1));
7878
assertThat(response.getResponses()[0].getFailure().getCause(), instanceOf(IndexNotFoundException.class));
79-
assertThat(response.getResponses()[0].getFailure().getCause().getMessage(), equalTo("no such index"));
79+
assertThat(response.getResponses()[0].getFailure().getCause().getMessage(), equalTo("no such index [testX]"));
8080
}
8181

8282
public void testMultiTermVectorsWithVersion() throws Exception {

0 commit comments

Comments
 (0)