Check for the right error and warning structure

Change-Id: I7546ad959e93486b04785a5ae82b9fb05903f936
diff --git a/lib/Kalamar/Plugin/KalamarErrors.pm b/lib/Kalamar/Plugin/KalamarErrors.pm
index fd4a174..977ecc5 100644
--- a/lib/Kalamar/Plugin/KalamarErrors.pm
+++ b/lib/Kalamar/Plugin/KalamarErrors.pm
@@ -2,6 +2,41 @@
 use Mojo::Base 'Mojolicious::Plugin';
 
 
+# Notify types
+sub _notify {
+  my ($c, $json, $type, $notify_type) = @_;
+  my $msgs = $json->{$type};
+
+  return unless $msgs;
+
+  # wrong structure
+  unless (ref $msgs && ref $msgs eq 'ARRAY') {
+    $c->notify(error => 'Message structure failed');
+    return 1;
+  }
+
+  # Get errors
+  foreach my $m (@$msgs) {
+
+    # Error is correctly defined
+    if (ref $m && ref $m eq 'ARRAY') {
+      $c->notify(
+        $notify_type =>
+          ($m->[0] ? $m->[0] . ': ' : '') .
+          ($m->[1] || 'Unknown')
+        );
+    }
+
+    # Wrong structure
+    else {
+      $c->notify(error => 'Message structure failed');
+    };
+  };
+
+  return 1;
+};
+
+
 # Register error plugin
 sub register {
   my ($plugin, $mojo) = @_;
@@ -11,42 +46,16 @@
   $mojo->helper(
     notify_on_warnings => sub {
       my ($c, $json) = @_;
-
-      my $warnings = $json->{warnings};
-
-      return unless $warnings;
-
-      # TODO: Check for ref!
-      foreach my $w (@$warnings) {
-        $c->notify(
-          warn =>
-            ($w->[0] ? $w->[0] . ': ' : '') .
-            $w->[1]
-          );
-      };
-
-      return 1;
+      return _notify($c, $json, 'warnings', 'warning');
     }
   );
 
+
   # Notify on errors
   $mojo->helper(
     notify_on_errors => sub {
       my ($c, $json) = @_;
-
-      my $errors = $json->{errors};
-
-      return unless $errors;
-
-      foreach my $e (@$errors) {
-        $c->notify(
-          error =>
-            ($e->[0] ? $e->[0] . ': ' : '') .
-            ($e->[1] || 'Unknown')
-          );
-      };
-
-      return 1;
+      return _notify($c, $json, 'errors', 'error');
     }
   );
 
@@ -77,9 +86,6 @@
       if ($json) {
         $c->stash(api_response => $json);
 
-        # TODO:
-        #   Check for references of errors and warnings!
-
         # There are errors
         if ($c->notify_on_errors($json)) {
 
diff --git a/t/fixtures/response_matchinfo_brokenerr2_x_x_p0-1.json b/t/fixtures/response_matchinfo_brokenerr2_x_x_p0-1.json
new file mode 100644
index 0000000..63e2dc6
--- /dev/null
+++ b/t/fixtures/response_matchinfo_brokenerr2_x_x_p0-1.json
@@ -0,0 +1,8 @@
+{
+  "comment" : "q=[orth=das",
+  "status" : 417,
+  "json" : {
+    "@context" : "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+    "errors" : "Error 1"
+  }
+}
diff --git a/t/fixtures/response_matchinfo_brokenerr_x_x_p0-1.json b/t/fixtures/response_matchinfo_brokenerr_x_x_p0-1.json
new file mode 100644
index 0000000..1ac2217
--- /dev/null
+++ b/t/fixtures/response_matchinfo_brokenerr_x_x_p0-1.json
@@ -0,0 +1,8 @@
+{
+  "comment" : "q=[orth=das",
+  "status" : 409,
+  "json" : {
+    "@context" : "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+    "errors" : [2, "Warning 3"]
+  }
+}
diff --git a/t/fixtures/response_matchinfo_brokenwarn_x_x_p0-1.json b/t/fixtures/response_matchinfo_brokenwarn_x_x_p0-1.json
new file mode 100644
index 0000000..bdd1d79
--- /dev/null
+++ b/t/fixtures/response_matchinfo_brokenwarn_x_x_p0-1.json
@@ -0,0 +1,11 @@
+{
+  "comment" : "q=[orth=das",
+  "status" : 200,
+  "json" : {
+    "@context" : "http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+    "warnings" : [
+      [1, "Warning 1"],
+      "Warning 2"
+    ]
+  }
+}
diff --git a/t/match_info.t b/t/match_info.t
index 1b6f8ef..58191a5 100644
--- a/t/match_info.t
+++ b/t/match_info.t
@@ -69,6 +69,26 @@
   ->json_is('/notifications/0/1', '404: Not Found')
   ;
 
+$t->get_ok('/corpus2/brokenerr/X/X/p0-1')
+  ->status_is(409)
+  ->json_is('/notifications/0/0', 'error')
+  ->json_is('/notifications/0/1', 'Message structure failed')
+  ;
+
+$t->get_ok('/corpus2/brokenwarn/X/X/p0-1')
+  ->status_is(200)
+  ->json_is('/notifications/0/0', 'warning')
+  ->json_is('/notifications/0/1', '1: Warning 1')
+  ->json_is('/notifications/1/0', 'error')
+  ->json_is('/notifications/1/1', 'Message structure failed')
+  ;
+
+$t->get_ok('/corpus2/brokenerr2/X/X/p0-1')
+  ->status_is(417)
+  ->json_is('/notifications/0/0', 'error')
+  ->json_is('/notifications/0/1', 'Message structure failed')
+  ;
+
 
 done_testing;
 __END__