Skip to content

Commit d419868

Browse files
dbwiddisopensearch-trigger-bot[bot]owaiskazi19
authored
[Backport 2.19] Use Bad Request status for InputCoercionException (#18161) (#18203)
* Use Bad Request status for InputCoercionException (#18161) * Use Bad Request status for InputCoercionException Signed-off-by: Daniel Widdis <[email protected]> * Add yamlRestTest for the fix Signed-off-by: Daniel Widdis <[email protected]> * Skip test before 3.1.0 Signed-off-by: Daniel Widdis <[email protected]> --------- Signed-off-by: Daniel Widdis <[email protected]> Co-authored-by: opensearch-trigger-bot[bot] <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> (cherry picked from commit 4ce638c) * Update CHANGELOG.md Co-authored-by: Owais Kazi <[email protected]> Signed-off-by: Daniel Widdis <[email protected]> --------- Signed-off-by: Daniel Widdis <[email protected]> Co-authored-by: opensearch-trigger-bot[bot] <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Co-authored-by: Owais Kazi <[email protected]>
1 parent b7aba2d commit d419868

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1414
### Removed
1515

1616
### Fixed
17+
- Use Bad Request status for InputCoercionException ([#18161](https://github.com/opensearch-project/OpenSearch/pull/18161))
1718

1819
### Security
1920

libs/core/src/main/java/org/opensearch/ExceptionsHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
package org.opensearch;
3434

3535
import com.fasterxml.jackson.core.JsonParseException;
36+
import com.fasterxml.jackson.core.exc.InputCoercionException;
3637

3738
import org.apache.logging.log4j.LogManager;
3839
import org.apache.logging.log4j.Logger;
@@ -97,6 +98,8 @@ public static RestStatus status(Throwable t) {
9798
return ((OpenSearchException) t).status();
9899
} else if (t instanceof IllegalArgumentException) {
99100
return RestStatus.BAD_REQUEST;
101+
} else if (t instanceof InputCoercionException) {
102+
return RestStatus.BAD_REQUEST;
100103
} else if (t instanceof JsonParseException) {
101104
return RestStatus.BAD_REQUEST;
102105
} else if (t instanceof OpenSearchRejectedExecutionException) {
@@ -112,6 +115,8 @@ public static String summaryMessage(Throwable t) {
112115
return getExceptionSimpleClassName(t) + "[" + t.getMessage() + "]";
113116
} else if (t instanceof IllegalArgumentException) {
114117
return "Invalid argument";
118+
} else if (t instanceof InputCoercionException) {
119+
return "Incompatible JSON value";
115120
} else if (t instanceof JsonParseException) {
116121
return "Failed to parse JSON";
117122
} else if (t instanceof OpenSearchRejectedExecutionException) {

rest-api-spec/src/main/resources/rest-api-spec/test/search/30_limits.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ setup:
4444
scroll: 5m
4545
size: 10010
4646

47+
---
48+
"Request with size exceeding max integer value":
49+
- skip:
50+
version: "- 3.0.99"
51+
reason: "returns 500 before 3.1.0"
52+
- do:
53+
catch: /Numeric value \(2147483648\) out of range of int/
54+
search:
55+
rest_total_hits_as_int: true
56+
index: test_1
57+
body:
58+
query:
59+
match_all: {}
60+
size: 2147483648
61+
62+
- match: { status: 400 }
63+
- match: { error.type: input_coercion_exception }
64+
4765
---
4866
"Rescore window limits":
4967
- do:

server/src/test/java/org/opensearch/ExceptionsHelperTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
package org.opensearch;
3434

3535
import com.fasterxml.jackson.core.JsonParseException;
36+
import com.fasterxml.jackson.core.exc.InputCoercionException;
3637

3738
import org.apache.commons.codec.DecoderException;
3839
import org.apache.lucene.index.CorruptIndexException;
@@ -110,12 +111,17 @@ private void assertError(final Throwable cause, final Error error) {
110111

111112
public void testStatus() {
112113
assertThat(ExceptionsHelper.status(new IllegalArgumentException("illegal")), equalTo(RestStatus.BAD_REQUEST));
114+
assertThat(ExceptionsHelper.status(new InputCoercionException(null, "illegal", null, null)), equalTo(RestStatus.BAD_REQUEST));
113115
assertThat(ExceptionsHelper.status(new JsonParseException(null, "illegal")), equalTo(RestStatus.BAD_REQUEST));
114116
assertThat(ExceptionsHelper.status(new OpenSearchRejectedExecutionException("rejected")), equalTo(RestStatus.TOO_MANY_REQUESTS));
115117
}
116118

117119
public void testSummaryMessage() {
118120
assertThat(ExceptionsHelper.summaryMessage(new IllegalArgumentException("illegal")), equalTo("Invalid argument"));
121+
assertThat(
122+
ExceptionsHelper.summaryMessage(new InputCoercionException(null, "illegal", null, null)),
123+
equalTo("Incompatible JSON value")
124+
);
119125
assertThat(ExceptionsHelper.summaryMessage(new JsonParseException(null, "illegal")), equalTo("Failed to parse JSON"));
120126
assertThat(ExceptionsHelper.summaryMessage(new OpenSearchRejectedExecutionException("rejected")), equalTo("Too many requests"));
121127
}

0 commit comments

Comments
 (0)