Skip to content

Commit 4eb1c00

Browse files
authored
Adjust analyze limit exception to be a bad_request (#116325) (#116495)
The exception is due to large input on the user and is resolvable by either the user adjusting their request or changing their cluster settings. So a user focused error is preferred. I chose bad_request as it seemed like the best fit. closes: #116323
1 parent fc120f7 commit 4eb1c00

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

docs/changelog/116325.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 116325
2+
summary: Adjust analyze limit exception to be a `bad_request`
3+
area: Analysis
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/action/admin/indices/analyze/TransportAnalyzeAction.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
1919
import org.apache.lucene.util.BytesRef;
2020
import org.elasticsearch.ElasticsearchException;
21+
import org.elasticsearch.ElasticsearchStatusException;
2122
import org.elasticsearch.action.support.ActionFilters;
2223
import org.elasticsearch.action.support.single.shard.TransportSingleShardAction;
2324
import org.elasticsearch.cluster.ClusterState;
@@ -44,6 +45,7 @@
4445
import org.elasticsearch.index.shard.ShardId;
4546
import org.elasticsearch.indices.IndicesService;
4647
import org.elasticsearch.injection.guice.Inject;
48+
import org.elasticsearch.rest.RestStatus;
4749
import org.elasticsearch.threadpool.ThreadPool;
4850
import org.elasticsearch.transport.TransportService;
4951

@@ -455,11 +457,12 @@ private TokenCounter(int maxTokenCount) {
455457
private void increment() {
456458
tokenCount++;
457459
if (tokenCount > maxTokenCount) {
458-
throw new IllegalStateException(
460+
throw new ElasticsearchStatusException(
459461
"The number of tokens produced by calling _analyze has exceeded the allowed maximum of ["
460462
+ maxTokenCount
461463
+ "]."
462-
+ " This limit can be set by changing the [index.analyze.max_token_count] index level setting."
464+
+ " This limit can be set by changing the [index.analyze.max_token_count] index level setting.",
465+
RestStatus.BAD_REQUEST
463466
);
464467
}
465468
}

server/src/test/java/org/elasticsearch/action/admin/indices/TransportAnalyzeActionTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.lucene.tests.analysis.MockTokenizer;
1414
import org.apache.lucene.util.automaton.Automata;
1515
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
16+
import org.elasticsearch.ElasticsearchStatusException;
1617
import org.elasticsearch.action.admin.indices.analyze.AnalyzeAction;
1718
import org.elasticsearch.action.admin.indices.analyze.TransportAnalyzeAction;
1819
import org.elasticsearch.cluster.metadata.IndexMetadata;
@@ -460,8 +461,8 @@ public void testExceedDefaultMaxTokenLimit() {
460461
AnalyzeAction.Request request = new AnalyzeAction.Request();
461462
request.text(text);
462463
request.analyzer("standard");
463-
IllegalStateException e = expectThrows(
464-
IllegalStateException.class,
464+
ElasticsearchStatusException e = expectThrows(
465+
ElasticsearchStatusException.class,
465466
() -> TransportAnalyzeAction.analyze(request, registry, null, maxTokenCount)
466467
);
467468
assertEquals(
@@ -477,8 +478,8 @@ public void testExceedDefaultMaxTokenLimit() {
477478
request2.text(text);
478479
request2.analyzer("standard");
479480
request2.explain(true);
480-
IllegalStateException e2 = expectThrows(
481-
IllegalStateException.class,
481+
ElasticsearchStatusException e2 = expectThrows(
482+
ElasticsearchStatusException.class,
482483
() -> TransportAnalyzeAction.analyze(request2, registry, null, maxTokenCount)
483484
);
484485
assertEquals(
@@ -506,8 +507,8 @@ public void testExceedSetMaxTokenLimit() {
506507
AnalyzeAction.Request request = new AnalyzeAction.Request();
507508
request.text(text);
508509
request.analyzer("standard");
509-
IllegalStateException e = expectThrows(
510-
IllegalStateException.class,
510+
ElasticsearchStatusException e = expectThrows(
511+
ElasticsearchStatusException.class,
511512
() -> TransportAnalyzeAction.analyze(request, registry, null, idxMaxTokenCount)
512513
);
513514
assertEquals(

0 commit comments

Comments
 (0)