SOLR-8836: Return 400, and a SolrException when an invalid json is provided to the update handler instead of 500.

This commit is contained in:
anshum 2016-03-15 10:55:03 -07:00
parent 0739f9155b
commit 4deb4cd1ba
3 changed files with 28 additions and 1 deletions

View File

@ -437,6 +437,9 @@ Other Changes
* SOLR-8799: Improve error message when tuple can't be read by SolrJ JDBC (Kevin Risden, Joel Bernstein)
* SOLR-8836: Return 400, and a SolrException when an invalid json is provided to the update handler
instead of 500. (Jason Gerlowski via Anshum Gupta)
================== 5.5.1 ==================
Bug Fixes

View File

@ -50,6 +50,7 @@ import org.apache.solr.update.RollbackUpdateCommand;
import org.apache.solr.update.processor.UpdateRequestProcessor;
import org.apache.solr.util.RecordingJSONParser;
import org.noggit.JSONParser;
import org.noggit.JSONParser.ParseException;
import org.noggit.ObjectBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -111,7 +112,10 @@ public class JsonLoader extends ContentStreamLoader {
}
this.processUpdate(reader);
} finally {
} catch (ParseException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cannot parse provided JSON: " + e.getMessage());
}
finally {
IOUtils.closeQuietly(reader);
}
}

View File

@ -181,6 +181,26 @@ public class JsonLoaderTest extends SolrTestCaseJ4 {
req.close();
}
@Test
public void testInvalidJsonProducesBadRequestSolrException() throws Exception
{
SolrQueryResponse rsp = new SolrQueryResponse();
BufferingRequestProcessor p = new BufferingRequestProcessor(null);
JsonLoader loader = new JsonLoader();
String invalidJsonString = "}{";
try(SolrQueryRequest req = req()) {
try {
loader.load(req, rsp, new ContentStreamBase.StringStream(invalidJsonString), p);
fail("Expected invalid JSON to produce a SolrException.");
} catch (SolrException expectedException) {
assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, expectedException.code());
assertTrue(expectedException.getMessage().contains("Cannot parse"));
assertTrue(expectedException.getMessage().contains("JSON"));
}
}
}
public void testSimpleFormatInAdd() throws Exception
{
String str = "{'add':[{'id':'1'},{'id':'2'}]}".replace('\'', '"');