Skip to content

Commit afa975d

Browse files
committed
Use Bad Request status for InputCoercionEcception
Signed-off-by: Daniel Widdis <[email protected]>
1 parent 3e94f0e commit afa975d

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
2828
- Fix simultaneously creating a snapshot and updating the repository can potentially trigger an infinite loop ([#17532](https://github.com/opensearch-project/OpenSearch/pull/17532))
2929
- Remove package org.opensearch.transport.grpc and replace with org.opensearch.plugin.transport.grpc ([#18031](https://github.com/opensearch-project/OpenSearch/pull/18031))
3030
- Fix the native plugin installation error cause by the pgp public key change ([#18147](https://github.com/opensearch-project/OpenSearch/pull/18147))
31+
- Use Bad Request status for InputCoercionEcception ([#18161](https://github.com/opensearch-project/OpenSearch/pull/18161))
3132

3233
### Security
3334

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;
@@ -98,6 +99,8 @@ public static RestStatus status(Throwable t) {
9899
return ((OpenSearchException) t).status();
99100
} else if (t instanceof IllegalArgumentException) {
100101
return RestStatus.BAD_REQUEST;
102+
} else if (t instanceof InputCoercionException) {
103+
return RestStatus.BAD_REQUEST;
101104
} else if (t instanceof JsonParseException) {
102105
return RestStatus.BAD_REQUEST;
103106
} else if (t instanceof OpenSearchRejectedExecutionException) {
@@ -115,6 +118,8 @@ public static String summaryMessage(Throwable t) {
115118
return getExceptionSimpleClassName(t) + "[" + t.getMessage() + "]";
116119
} else if (t instanceof IllegalArgumentException) {
117120
return "Invalid argument";
121+
} else if (t instanceof InputCoercionException) {
122+
return "Incompatible JSON value";
118123
} else if (t instanceof JsonParseException) {
119124
return "Failed to parse JSON";
120125
} else if (t instanceof OpenSearchRejectedExecutionException) {

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)