Catch timeouts in proxy

Change-Id: If015c50aafd79fa7aaa914da3dcd335a4c07c3b0
diff --git a/Changes b/Changes
index b247f4b..f7579c5 100755
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.37 2019-12-11
+0.37 2019-12-17
         - Removed deprecated 'kalamar_test_port' helper.
         - Separated KalamarHelpers and KalamarPages.
         - Renamed 'doc_link_to' to 'embedded_link_to'
@@ -24,6 +24,7 @@
         - Implemented preliminary host->plugin communication.
         - Fixed matchID transformation to deal with textSigles
           having dashes.
+        - Catch timeouts in proxy.
 
 0.36 2019-09-19
         - Rename all cookies to be independent
diff --git a/lib/Kalamar/Controller/Proxy.pm b/lib/Kalamar/Controller/Proxy.pm
index aff9d8a..af1eb75 100644
--- a/lib/Kalamar/Controller/Proxy.pm
+++ b/lib/Kalamar/Controller/Proxy.pm
@@ -39,7 +39,17 @@
     before_korap_request => ($c, $tx)
   );
 
-  $c->proxy->start_p($tx)->wait;
+  # Start proxy transaction and cache failure
+  $c->proxy->start_p($tx)->catch(
+    sub {
+      my $err = shift;
+      $c->render(
+        text => "Proxy error: $err",
+        status => 400
+      );
+    }
+  )->wait;
+
   $tx->res->content->once(
     body => sub {
       my $headers = $c->res->headers;
@@ -53,6 +63,8 @@
       $c->app->plugins->emit_hook(after_render => $c);
     }
   );
+
+  return 1;
 };
 
 1;
diff --git a/t/proxy.t b/t/proxy.t
index 2e2f922..41b0acf 100644
--- a/t/proxy.t
+++ b/t/proxy.t
@@ -28,7 +28,8 @@
   }
 );
 # Configure fake backend
-$fake_backend->pattern->defaults->{app}->log($t->app->log);
+my $fake_app = $fake_backend->pattern->defaults->{app};
+$fake_app->log($t->app->log);
 
 # Globally set server
 $t->app->ua->server->app($t->app);
@@ -90,6 +91,26 @@
   ->json_is('/token_type', 'Bearer')
   ;
 
+# Create long-running route
+$fake_app->routes->get('/v1.0/longwait')->to(
+  cb => sub {
+    my $c = shift;
+
+    $c->render_later;
+    Mojo::IOLoop->timer(
+      5 => sub {
+        return $c->render(text => 'okay');
+      }
+    );
+  });
+
+# Set proxy timeout
+$t->app->ua->inactivity_timeout(1);
+
+$t->get_ok('/api/v1.0/longwait')
+  ->status_is(400)
+  ->content_is('Proxy error: Inactivity timeout')
+  ;
 
 done_testing;
 __END__