Improve error handling

Change-Id: I1f54cf9cd4770d6602f70036cf0e27c9ede8c893
diff --git a/t/fixtures/fake_backend.pl b/t/fixtures/fake_backend.pl
index 89ad3c3..9f06697 100644
--- a/t/fixtures/fake_backend.pl
+++ b/t/fixtures/fake_backend.pl
@@ -36,6 +36,8 @@
   my $c = shift;
   my $q_name = shift;
   my $file = $fixture_path->child("response_$q_name.json");
+  $c->app->log->debug("Load response from $file");
+
   unless (-f $file) {
     return {
       status => 500,
@@ -44,6 +46,7 @@
       }
     }
   };
+
   my $response = $file->slurp;
   return decode_json($response);
 };
@@ -64,6 +67,8 @@
   $v->optional('ql');
   $v->optional('count');
   $v->optional('context');
+  $v->optional('offset');
+  $v->optional('cutoff')->in(qw/true false/);
 
   $c->app->log->debug('Receive request');
 
@@ -78,11 +83,16 @@
   };
 
   if (!$v->param('q')) {
-    return $c->render(%{$c->load_response('no_query')});
+    return $c->render(%{$c->load_response('query_no_query')});
   };
 
+  my @slug_base = ($v->param('q'));
+  push @slug_base, 'o' . $v->param('offset') if defined $v->param('offset');
+  push @slug_base, 'c' . $v->param('count') if defined $v->param('count');
+  push @slug_base, 'co' . $v->param('cutoff') if defined $v->param('cutoff');
+
   # Get response based on query parameter
-  my $response = $c->load_response(slugify($v->param('q')));
+  my $response = $c->load_response('query_' . slugify(join('_', @slug_base)));
 
   # Check authentification
   if (my $auth = $c->req->headers->header('Authorization')) {
diff --git a/t/fixtures/response_query_baum_o0_c25_cotrue.json b/t/fixtures/response_query_baum_o0_c25_cotrue.json
new file mode 100644
index 0000000..6e0921e
--- /dev/null
+++ b/t/fixtures/response_query_baum_o0_c25_cotrue.json
@@ -0,0 +1,77 @@
+{
+  "status" : 200,
+  "json" : {
+    "@context" : "http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld",
+    "meta" :  {
+      "count" : 25,
+      "startIndex" : 0,
+      "cutOff": true,
+      "authorized" : null,
+      "timeout" : 120000,
+      "context" : {
+        "left" : ["token",40],
+        "right" : ["token",40]
+      },
+      "fields" : ["pubDate","subTitle","author","pubPlace","title","textSigle","UID","ID","layerInfos","corpusSigle","docSigle","corpusID","textClass"],
+      "version" : "0.55.7",
+      "benchmark" : "0.120577834 s",
+      "totalResults" : -1,
+      "serialQuery" : "tokens:s:Baum",
+      "itemsPerPage" : 25
+    },
+    "query" : {
+      "@type" : "koral:token",
+      "wrap" : {
+        "@type" : "koral:term",
+        "layer" : "orth",
+        "key" : "Baum",
+        "match" : "match:eq",
+        "foundry" : "opennlp",
+        "rewrites" : [
+          {
+            "@type" : "koral:rewrite",
+            "src" : "Kustvakt",
+            "operation" : "operation:injection",
+            "scope" : "foundry"
+          }
+        ]
+      }
+    },
+    "matches" : [
+      {
+        "field" : "tokens",
+        "pubPlace" : "München",
+        "textSigle" : "GOE/AGI/00000",
+        "docSigle" : "GOE/AGI",
+        "corpusSigle" : "GOE",
+        "title" : "Italienische Reise",
+        "subTitle" : "Auch ich in Arkadien!",
+        "author" : "Goethe, Johann Wolfgang von",
+        "layerInfos" : "base/s=spans corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels mdp/d=rels opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens tt/s=spans",
+        "startMore" : true,
+        "endMore" : true,
+        "snippet" : "<span class=\"context-left\"><span class=\"more\"></span>sie etwas bedeuten zu wollen und machte mit der Oberlippe eine fatale Miene. ich sprach sehr viel mit ihr durch, sie war überall zu Hause und merkte gut auf die Gegenstände. so fragte sie mich einmal, was das für ein </span><span class=\"match\"><mark>Baum</mark></span><span class=\"context-right\"> sei. es war ein schöner großer Ahorn, der erste, der mir auf der ganzen Reise zu Gesichte kam. den hatte sie doch gleich bemerkt und freute sich, da mehrere nach und nach erschienen, daß sie auch diesen Baum unterscheiden könne<span class=\"more\"></span></span>",
+        "matchID" : "match-GOE/AGI/00000-p2030-2031",
+        "UID" : 0,
+        "pubDate" : "1982"
+      },
+      {
+        "field" : "tokens",
+        "pubPlace" : "München",
+        "textSigle" : "GOE/AGI/00001",
+        "docSigle" : "GOE/AGI",
+        "corpusSigle" : "GOE",
+        "title" : "Italienische Reise",
+        "subTitle" : "Auch ich in Arkadien!",
+        "author" : "Goethe, Johann Wolfgang von",
+        "layerInfos" : "base/s=spans corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels mdp/d=rels opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens tt/s=spans",
+        "startMore" : true,
+        "endMore" : true,
+        "snippet" : "<span class=\"context-left\"><span class=\"more\"></span>für ein Baum sei. es war ein schöner großer Ahorn, der erste, der mir auf der ganzen Reise zu Gesichte kam. den hatte sie doch gleich bemerkt und freute sich, da mehrere nach und nach erschienen, daß sie auch diesen </span><span class=\"match\"><mark>Baum</mark></span><span class=\"context-right\"> unterscheiden könne. sie gehe, sagte sie, nach Bozen auf die Messe, wo ich doch wahrscheinlich auch hinzöge. wenn sie mich dort anträfe, müsse ich ihr einen Jahrmarkt kaufen, welches ich ihr denn auch versprach. dort wollte sie auch ihre neue<span class=\"more\"></span></span>",
+        "matchID" : "match-GOE/AGI/00000-p2068-2069",
+        "UID" : 0,
+        "pubDate" : "1982"
+      }
+    ]
+  }
+}
diff --git a/t/fixtures/response_query_der_o0_c2.json b/t/fixtures/response_query_der_o0_c2.json
new file mode 100644
index 0000000..f712139
--- /dev/null
+++ b/t/fixtures/response_query_der_o0_c2.json
@@ -0,0 +1,4 @@
+{
+  "status" : 200,
+  "json" : {"@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld","meta":{"count":2,"startIndex":0,"timeout":120000,"context":{"left":["token",6],"right":["token",6]},"fields":["textSigle","author","docSigle","availability","title","pubDate","UID","corpusID","textClass","subTitle","layerInfos","ID","pubPlace","corpusSigle"],"version":"0.58.0","benchmark":"22.347006999999998 ms","totalResults":14581,"serialQuery":"tokens:s:der","itemsPerPage":2},"query":{"@type":"koral:token","wrap":{"@type":"koral:term","match":"match:eq","layer":"orth","key":"der","foundry":"opennlp","rewrites":[{"@type":"koral:rewrite","src":"Kustvakt","operation":"operation:injection","scope":"foundry"}]}},"matches":[{"field":"tokens","pubPlace":"München","textSigle":"GOE/AGI/00000","docSigle":"GOE/AGI","corpusSigle":"GOE","title":"Italienische Reise","subTitle":"Auch ich in Arkadien!","author":"Goethe, Johann Wolfgang von","availability":"ACA-NC","layerInfos":"corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens","startMore":true,"endMore":true,"license":"ACA-NC","snippet":"<span class=\"context-left\"><span class=\"more\"></span>das Stift Waldsassen entgegen - köstliche Besitztümer </span><span class=\"match\"><mark>der</mark></span><span class=\"context-right\"> geistlichen Herren, die früher als andere<span class=\"more\"></span></span>","matchID":"match-GOE/AGI/00000-p161-162","pubDate":"1982","UID":0},{"field":"tokens","pubPlace":"München","textSigle":"GOE/AGI/00000","docSigle":"GOE/AGI","corpusSigle":"GOE","title":"Italienische Reise","subTitle":"Auch ich in Arkadien!","author":"Goethe, Johann Wolfgang von","availability":"ACA-NC","layerInfos":"corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens","startMore":true,"endMore":true,"license":"ACA-NC","snippet":"<span class=\"context-left\"><span class=\"more\"></span>Kloster im Lande weit umher Besitzungen. </span><span class=\"match\"><mark>der</mark></span><span class=\"context-right\"> Boden ist aufgelöster Tonschiefer. der Quarz<span class=\"more\"></span></span>","matchID":"match-GOE/AGI/00000-p200-201","pubDate":"1982","UID":0}]}
+}
diff --git a/t/fixtures/response_query_der_o0_c2_cotrue.json b/t/fixtures/response_query_der_o0_c2_cotrue.json
new file mode 100644
index 0000000..a683805
--- /dev/null
+++ b/t/fixtures/response_query_der_o0_c2_cotrue.json
@@ -0,0 +1,4 @@
+{
+  "status" : 200,
+  "json" : {"@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld","meta":{"cutOff":true,"count":2,"startIndex":0,"timeout":120000,"context":{"left":["token",6],"right":["token",6]},"fields":["textSigle","author","docSigle","availability","title","pubDate","UID","corpusID","textClass","subTitle","layerInfos","ID","pubPlace","corpusSigle"],"version":"0.58.0","benchmark":"29.377798 ms","totalResults":-1,"serialQuery":"tokens:s:der","itemsPerPage":2},"query":{"@type":"koral:token","wrap":{"@type":"koral:term","match":"match:eq","layer":"orth","key":"der","foundry":"opennlp","rewrites":[{"@type":"koral:rewrite","src":"Kustvakt","operation":"operation:injection","scope":"foundry"}]}},"matches":[{"field":"tokens","pubPlace":"München","textSigle":"GOE/AGI/00000","docSigle":"GOE/AGI","corpusSigle":"GOE","title":"Italienische Reise","subTitle":"Auch ich in Arkadien!","author":"Goethe, Johann Wolfgang von","availability":"ACA-NC","layerInfos":"corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens","startMore":true,"endMore":true,"license":"ACA-NC","snippet":"<span class=\"context-left\"><span class=\"more\"></span>das Stift Waldsassen entgegen - köstliche Besitztümer </span><span class=\"match\"><mark>der</mark></span><span class=\"context-right\"> geistlichen Herren, die früher als andere<span class=\"more\"></span></span>","matchID":"match-GOE/AGI/00000-p161-162","pubDate":"1982","UID":0},{"field":"tokens","pubPlace":"München","textSigle":"GOE/AGI/00000","docSigle":"GOE/AGI","corpusSigle":"GOE","title":"Italienische Reise","subTitle":"Auch ich in Arkadien!","author":"Goethe, Johann Wolfgang von","availability":"ACA-NC","layerInfos":"corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens","startMore":true,"endMore":true,"license":"ACA-NC","snippet":"<span class=\"context-left\"><span class=\"more\"></span>Kloster im Lande weit umher Besitzungen. </span><span class=\"match\"><mark>der</mark></span><span class=\"context-right\"> Boden ist aufgelöster Tonschiefer. der Quarz<span class=\"more\"></span></span>","matchID":"match-GOE/AGI/00000-p200-201","pubDate":"1982","UID":0}]}
+}
diff --git a/t/fixtures/response_query_der_o2_c2.json b/t/fixtures/response_query_der_o2_c2.json
new file mode 100644
index 0000000..ae77282
--- /dev/null
+++ b/t/fixtures/response_query_der_o2_c2.json
@@ -0,0 +1,4 @@
+{
+  "status" : 200,
+  "json" : {"@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld","meta":{"count":2,"startIndex":2,"timeout":120000,"context":{"left":["token",6],"right":["token",6]},"fields":["textSigle","author","docSigle","availability","title","pubDate","UID","corpusID","textClass","subTitle","layerInfos","ID","pubPlace","corpusSigle"],"version":"0.58.0","benchmark":"31.207413 ms","totalResults":14581,"serialQuery":"tokens:s:der","itemsPerPage":2},"query":{"@type":"koral:token","wrap":{"@type":"koral:term","match":"match:eq","layer":"orth","key":"der","foundry":"opennlp","rewrites":[{"@type":"koral:rewrite","src":"Kustvakt","operation":"operation:injection","scope":"foundry"}]}},"matches":[{"field":"tokens","pubPlace":"München","textSigle":"GOE/AGI/00000","docSigle":"GOE/AGI","corpusSigle":"GOE","title":"Italienische Reise","subTitle":"Auch ich in Arkadien!","author":"Goethe, Johann Wolfgang von","availability":"ACA-NC","layerInfos":"corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens","startMore":true,"endMore":true,"license":"ACA-NC","snippet":"<span class=\"context-left\"><span class=\"more\"></span>Besitzungen. der Boden ist aufgelöster Tonschiefer. </span><span class=\"match\"><mark>der</mark></span><span class=\"context-right\"> Quarz, der sich in dieser Gebirgsart<span class=\"more\"></span></span>","matchID":"match-GOE/AGI/00000-p205-206","pubDate":"1982","UID":0},{"field":"tokens","pubPlace":"München","textSigle":"GOE/AGI/00000","docSigle":"GOE/AGI","corpusSigle":"GOE","title":"Italienische Reise","subTitle":"Auch ich in Arkadien!","author":"Goethe, Johann Wolfgang von","availability":"ACA-NC","layerInfos":"corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens","startMore":true,"endMore":true,"license":"ACA-NC","snippet":"<span class=\"context-left\"><span class=\"more\"></span>Boden ist aufgelöster Tonschiefer. der Quarz, </span><span class=\"match\"><mark>der</mark></span><span class=\"context-right\"> sich in dieser Gebirgsart befindet und<span class=\"more\"></span></span>","matchID":"match-GOE/AGI/00000-p207-208","pubDate":"1982","UID":0}]}
+}
diff --git a/t/fixtures/response_query_der_o2_c2_cotrue.json b/t/fixtures/response_query_der_o2_c2_cotrue.json
new file mode 100644
index 0000000..c0ef128
--- /dev/null
+++ b/t/fixtures/response_query_der_o2_c2_cotrue.json
@@ -0,0 +1,4 @@
+{
+  "status" : 200,
+  "json" : {"@context":"http://korap.ids-mannheim.de/ns/KoralQuery/v0.3/context.jsonld","meta":{"cutOff":true,"count":2,"startIndex":2,"timeout":120000,"context":{"left":["token",6],"right":["token",6]},"fields":["textSigle","author","docSigle","availability","title","pubDate","UID","corpusID","textClass","subTitle","layerInfos","ID","pubPlace","corpusSigle"],"version":"0.58.0","benchmark":"21.959009 ms","totalResults":-1,"serialQuery":"tokens:s:der","itemsPerPage":2},"query":{"@type":"koral:token","wrap":{"@type":"koral:term","match":"match:eq","layer":"orth","key":"der","foundry":"opennlp","rewrites":[{"@type":"koral:rewrite","src":"Kustvakt","operation":"operation:injection","scope":"foundry"}]}},"matches":[{"field":"tokens","pubPlace":"München","textSigle":"GOE/AGI/00000","docSigle":"GOE/AGI","corpusSigle":"GOE","title":"Italienische Reise","subTitle":"Auch ich in Arkadien!","author":"Goethe, Johann Wolfgang von","availability":"ACA-NC","layerInfos":"corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens","startMore":true,"endMore":true,"license":"ACA-NC","snippet":"<span class=\"context-left\"><span class=\"more\"></span>Besitzungen. der Boden ist aufgelöster Tonschiefer. </span><span class=\"match\"><mark>der</mark></span><span class=\"context-right\"> Quarz, der sich in dieser Gebirgsart<span class=\"more\"></span></span>","matchID":"match-GOE/AGI/00000-p205-206","pubDate":"1982","UID":0},{"field":"tokens","pubPlace":"München","textSigle":"GOE/AGI/00000","docSigle":"GOE/AGI","corpusSigle":"GOE","title":"Italienische Reise","subTitle":"Auch ich in Arkadien!","author":"Goethe, Johann Wolfgang von","availability":"ACA-NC","layerInfos":"corenlp/c=spans corenlp/p=tokens corenlp/s=spans dereko/s=spans malt/d=rels marmot/m=tokens marmot/p=tokens opennlp/p=tokens opennlp/s=spans tt/l=tokens tt/p=tokens","startMore":true,"endMore":true,"license":"ACA-NC","snippet":"<span class=\"context-left\"><span class=\"more\"></span>Boden ist aufgelöster Tonschiefer. der Quarz, </span><span class=\"match\"><mark>der</mark></span><span class=\"context-right\"> sich in dieser Gebirgsart befindet und<span class=\"more\"></span></span>","matchID":"match-GOE/AGI/00000-p207-208","pubDate":"1982","UID":0}]}
+}