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.
This commit is contained in:
Rory Hunter 2019-11-26 12:36:56 +00:00 committed by GitHub
parent 41daf284f5
commit cf5f013033
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 0 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch; package org.elasticsearch;
import com.fasterxml.jackson.core.JsonParseException;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.CorruptIndexException;
@ -71,6 +72,8 @@ public final class ExceptionsHelper {
return ((ElasticsearchException) t).status(); return ((ElasticsearchException) t).status();
} else if (t instanceof IllegalArgumentException) { } else if (t instanceof IllegalArgumentException) {
return RestStatus.BAD_REQUEST; return RestStatus.BAD_REQUEST;
} else if (t instanceof JsonParseException) {
return RestStatus.BAD_REQUEST;
} else if (t instanceof EsRejectedExecutionException) { } else if (t instanceof EsRejectedExecutionException) {
return RestStatus.TOO_MANY_REQUESTS; return RestStatus.TOO_MANY_REQUESTS;
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch; package org.elasticsearch;
import com.fasterxml.jackson.core.JsonParseException;
import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.DecoderException;
import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.CorruptIndexException;
import org.elasticsearch.action.OriginalIndices; import org.elasticsearch.action.OriginalIndices;
@ -95,6 +96,7 @@ public class ExceptionsHelperTests extends ESTestCase {
public void testStatus() { public void testStatus() {
assertThat(ExceptionsHelper.status(new IllegalArgumentException("illegal")), equalTo(RestStatus.BAD_REQUEST)); assertThat(ExceptionsHelper.status(new IllegalArgumentException("illegal")), equalTo(RestStatus.BAD_REQUEST));
assertThat(ExceptionsHelper.status(new JsonParseException(null, "illegal")), equalTo(RestStatus.BAD_REQUEST));
assertThat(ExceptionsHelper.status(new EsRejectedExecutionException("rejected")), equalTo(RestStatus.TOO_MANY_REQUESTS)); assertThat(ExceptionsHelper.status(new EsRejectedExecutionException("rejected")), equalTo(RestStatus.TOO_MANY_REQUESTS));
} }