lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ans...@apache.org
Subject lucene-solr:master: SOLR-8836: Return 400, and a SolrException when an invalid json is provided to the update handler instead of 500.
Date Tue, 15 Mar 2016 18:15:30 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/master 870baafc8 -> 30a77b73b


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


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/30a77b73
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/30a77b73
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/30a77b73

Branch: refs/heads/master
Commit: 30a77b73b603ba52a50da397aefc6f9a88f05732
Parents: 870baaf
Author: anshum <anshum@apache.org>
Authored: Tue Mar 15 10:55:03 2016 -0700
Committer: anshum <anshum@apache.org>
Committed: Tue Mar 15 10:55:20 2016 -0700

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  3 +++
 .../apache/solr/handler/loader/JsonLoader.java  |  6 +++++-
 .../org/apache/solr/handler/JsonLoaderTest.java | 20 ++++++++++++++++++++
 3 files changed, 28 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/30a77b73/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 2014020..691e87f 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -425,6 +425,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

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/30a77b73/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java b/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java
index e99b243..ba800ff 100644
--- a/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java
+++ b/solr/core/src/java/org/apache/solr/handler/loader/JsonLoader.java
@@ -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);
       }
     }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/30a77b73/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java b/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java
index f2316da..e27ca1a 100644
--- a/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java
+++ b/solr/core/src/test/org/apache/solr/handler/JsonLoaderTest.java
@@ -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('\'', '"');


Mime
View raw message