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:
parent
41daf284f5
commit
cf5f013033
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.elasticsearch;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.lucene.index.CorruptIndexException;
|
||||
|
@ -71,6 +72,8 @@ public final class ExceptionsHelper {
|
|||
return ((ElasticsearchException) t).status();
|
||||
} else if (t instanceof IllegalArgumentException) {
|
||||
return RestStatus.BAD_REQUEST;
|
||||
} else if (t instanceof JsonParseException) {
|
||||
return RestStatus.BAD_REQUEST;
|
||||
} else if (t instanceof EsRejectedExecutionException) {
|
||||
return RestStatus.TOO_MANY_REQUESTS;
|
||||
}
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.elasticsearch;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParseException;
|
||||
import org.apache.commons.codec.DecoderException;
|
||||
import org.apache.lucene.index.CorruptIndexException;
|
||||
import org.elasticsearch.action.OriginalIndices;
|
||||
|
@ -95,6 +96,7 @@ public class ExceptionsHelperTests extends ESTestCase {
|
|||
|
||||
public void testStatus() {
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue