Do not pass broken queries to Krill

Change-Id: I7f6d6f949ad4b4cc4ce1febf523cedb982bec45f
diff --git a/full/Changes b/full/Changes
index 738c78f..bdf5134 100644
--- a/full/Changes
+++ b/full/Changes
@@ -1,5 +1,5 @@
 version 0.60.4
-04/07/2018
+05/07/2018
     - implemented OAuth2 authorization code request with OpenID Authentication (margaretha)
     - enabled OAuth2 authorization without OpenID authentication using Nimbus library (margaretha)
     - implemented response handler for OpenID authentication errors in authorization requests (margaretha)
@@ -20,6 +20,7 @@
     - fixed OAuth2 client unique URL-hashcode (margaretha)
     - migrated logging to log4j 2 and adapted java.util.logging to log4j(margaretha)
     - Added support for unrestricted corpus statistics (ndiewald)
+    - Do not pass broken queries to Krill (diewald)
     
 version 0.60.3
 06/06/2018
diff --git a/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java b/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
index 0b345cb..d78eaea 100644
--- a/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
+++ b/full/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
@@ -444,6 +444,14 @@
                 pageLength, cutoff);
         serializer.setMeta(meta.raw());
 
+		// There is an error in query processing
+		// - either query, corpus or meta
+		if (serializer.hasErrors()) {
+
+			// Do not pass further to backend
+			return Response.status(Response.Status.BAD_REQUEST).entity(serializer.toJSON()).build();		
+		};
+		
         String query;
         try {
             query = this.processor.processQuery(serializer.toJSON(), user);
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
index ecede59..13836be 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
@@ -4,6 +4,7 @@
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
 
 import javax.ws.rs.core.MediaType;
 
@@ -42,7 +43,6 @@
         assertEquals(ClientResponse.Status.OK.getStatusCode(),
                 response.getStatus());
         String ent = response.getEntity(String.class);
-//        System.out.println(ent);
         JsonNode node = JsonUtils.readTree(ent);
         assertNotNull(node);
         assertEquals("koral:doc", node.at("/collection/@type").asText());
@@ -54,6 +54,28 @@
                 node.at("/collection/rewrites/0/operation").asText());
     }
 
+	
+    @Test
+    public void testSearchQueryFailure () throws KustvaktException{
+        ClientResponse response = resource()
+			.path("search").queryParam("q", "[orth=der")
+			.queryParam("ql", "poliqarp")
+			.queryParam("cq", "corpusSigle=WPD | corpusSigle=GOE")
+			.queryParam("count", "13")
+			.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                response.getStatus());
+
+        String ent = response.getEntity(String.class);
+		JsonNode node = JsonUtils.readTree(ent);
+        assertNotNull(node);
+        assertEquals(302, node.at("/errors/0/0").asInt());
+        assertEquals(302, node.at("/errors/1/0").asInt());
+		assertTrue(node.at("/errors/2").isMissingNode());
+		assertFalse(node.at("/collection").isMissingNode());
+        assertEquals(13, node.at("/meta/count").asInt());
+    }
+
 
     @Test
     public void testSearchQueryWithMeta () throws KustvaktException{