Do not pass broken queries to Krill

Change-Id: I7f6d6f949ad4b4cc4ce1febf523cedb982bec45f
diff --git a/lite/Changes b/lite/Changes
index bd0f1db..03c57d6 100644
--- a/lite/Changes
+++ b/lite/Changes
@@ -1,6 +1,7 @@
 version 0.60.2
-29/06/2018
+05/07/2018
     - Added support for unrestricted corpus statistics (ndiewald)
+    - Do not pass broken queries to Krill (diewald)
 
 version 0.60.1
 12/06/2018
diff --git a/lite/src/main/java/de/ids_mannheim/korap/web/service/lite/LiteService.java b/lite/src/main/java/de/ids_mannheim/korap/web/service/lite/LiteService.java
index 3a384d5..0ed2b95 100644
--- a/lite/src/main/java/de/ids_mannheim/korap/web/service/lite/LiteService.java
+++ b/lite/src/main/java/de/ids_mannheim/korap/web/service/lite/LiteService.java
@@ -24,6 +24,8 @@
 
 import com.sun.jersey.core.util.MultivaluedMapImpl;
 
+import com.fasterxml.jackson.databind.JsonNode;
+
 import de.ids_mannheim.korap.config.KustvaktConfiguration;
 import de.ids_mannheim.korap.config.QueryBuilderUtil;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
@@ -35,6 +37,7 @@
 import de.ids_mannheim.korap.web.ClientsHandler;
 import de.ids_mannheim.korap.web.CoreResponseHandler;
 import de.ids_mannheim.korap.web.SearchKrill;
+import de.ids_mannheim.korap.utils.JsonUtils;
 
 /**
  * @author hanl
@@ -144,17 +147,18 @@
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
         }
-        // todo: should be possible to add the meta part to the query serialization
-        jlog.info("Serialized search: "+jsonld);
-        try {
-            String result = searchKrill.search(jsonld);
-            jlog.debug("The result set: "+result);
-            return Response.ok(result).build();
-        }
-        catch (Exception e) {
-            e.printStackTrace();
-        }
-        return Response.ok().build();
+
+		// todo: should be possible to add the meta part to the query serialization
+		jlog.info("Serialized search: "+jsonld);
+		try {
+			String result = searchKrill.search(jsonld);
+			jlog.debug("The result set: "+result);
+			return Response.ok(result).build();
+		}
+		catch (Exception e) {
+			e.printStackTrace();
+		};
+		return Response.ok().build();
     }
 
 
@@ -181,6 +185,15 @@
         serializer.setMeta(meta.raw());
         if (cq != null) serializer.setCollection(cq);
 
+
+		// 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(), null);
@@ -188,7 +201,8 @@
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
         }
-        jlog.info("the serialized query "+ query);
+
+		jlog.info("the serialized query "+ query);
 
         // This may not work with the the KoralQuery
         if (eng.equals(KustvaktConfiguration.BACKENDS.NEO4J)) {
diff --git a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteServiceTest.java b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteServiceTest.java
index 8c815e8..42f6ebc 100644
--- a/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteServiceTest.java
+++ b/lite/src/test/java/de/ids_mannheim/korap/web/service/LiteServiceTest.java
@@ -3,6 +3,8 @@
 import static org.junit.Assert.assertEquals;
 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 java.util.concurrent.ThreadLocalRandom;
 import java.util.*;
@@ -168,6 +170,27 @@
         assertNotEquals(0, node.at("/matches").size());
     }
 
+	@Test
+    public void testQueryFailure () throws KustvaktException{
+        ClientResponse response = resource()
+                .path("search").queryParam("q", "[orth=das")
+                .queryParam("ql", "poliqarp")
+                .queryParam("cq", "corpusSigle=WPD | corpusSigle=GOE")
+			.queryParam("count", "13")
+			.get(ClientResponse.class);
+        assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
+                response.getStatus());
+        String query = response.getEntity(String.class);
+
+		JsonNode node = JsonUtils.readTree(query);
+        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 testFoundryRewrite () throws KustvaktException{