Skip to content

Commit cf5f013

Browse files
authored
Return 400 when handling invalid JSON (#49558)
Backport of #49552. Closes #49428. The code that works out an HTTP code for an exception didn't consider the JsonParseException case, meant that an invalid JSON request could result in a 500 Internal Server Error. Now it returns 400 Bad Request.
1 parent 41daf28 commit cf5f013

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

server/src/main/java/org/elasticsearch/ExceptionsHelper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch;
2121

22+
import com.fasterxml.jackson.core.JsonParseException;
2223
import org.apache.logging.log4j.LogManager;
2324
import org.apache.logging.log4j.Logger;
2425
import org.apache.lucene.index.CorruptIndexException;
@@ -71,6 +72,8 @@ public static RestStatus status(Throwable t) {
7172
return ((ElasticsearchException) t).status();
7273
} else if (t instanceof IllegalArgumentException) {
7374
return RestStatus.BAD_REQUEST;
75+
} else if (t instanceof JsonParseException) {
76+
return RestStatus.BAD_REQUEST;
7477
} else if (t instanceof EsRejectedExecutionException) {
7578
return RestStatus.TOO_MANY_REQUESTS;
7679
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch;
2121

22+
import com.fasterxml.jackson.core.JsonParseException;
2223
import org.apache.commons.codec.DecoderException;
2324
import org.apache.lucene.index.CorruptIndexException;
2425
import org.elasticsearch.action.OriginalIndices;
@@ -95,6 +96,7 @@ private void assertError(final Throwable cause, final Error error) {
9596

9697
public void testStatus() {
9798
assertThat(ExceptionsHelper.status(new IllegalArgumentException("illegal")), equalTo(RestStatus.BAD_REQUEST));
99+
assertThat(ExceptionsHelper.status(new JsonParseException(null, "illegal")), equalTo(RestStatus.BAD_REQUEST));
98100
assertThat(ExceptionsHelper.status(new EsRejectedExecutionException("rejected")), equalTo(RestStatus.TOO_MANY_REQUESTS));
99101
}
100102

0 commit comments

Comments
 (0)