useful errors on misused middleware in route definitions param_decoding
Christian Walde [Tue, 8 Jan 2013 15:33:33 +0000 (16:33 +0100)]
lib/Web/Dispatch.pm
t/dispatch_misc.t

index 5da5854..d1b57c9 100644 (file)
@@ -121,6 +121,10 @@ sub _to_try {
   } elsif (!ref($try) and ref($more->[0]) eq 'CODE') {
     $self->_construct_node(match => $try, run => shift(@$more))->to_app;
   } elsif (
+    !ref($try) and blessed($more->[0]) and $more->[0]->isa('Plack::Middleware')
+  ) {
+    die "Middleware needs to be wrapped in a sub.";
+  } elsif (
     (blessed($try) && $try->isa('Web::Dispatch::Matcher'))
     and (ref($more->[0]) eq 'CODE')
   ) {
@@ -129,6 +133,8 @@ sub _to_try {
       match => $try,
       run => shift(@$more)
     })->to_app;
+  } elsif (blessed($try) && $try->isa('Plack::Middleware')) {
+    die "Middleware needs a route definition and has to be wrapped in a sub.";
   } elsif (blessed($try) && $try->can('to_app')) {
     $try->to_app;
   } else {
index 0c11f2c..36b213a 100644 (file)
@@ -26,6 +26,7 @@ broken_route_def();
 array_with_sub();
 array_with_no_sub();
 middleware_as_only_route();
+plain_middleware_with_route();
 route_returns_middleware_plus_extra();
 route_returns_undef();
 
@@ -115,7 +116,17 @@ sub middleware_as_only_route {
     my $get = run_request( GET => 'http://localhost/' );
 
     cmp_ok $get->code, '==', 500, "a route definition consisting of only a middleware causes a bail";
-    like $get->content, qr[Multiple results but first one is a middleware \(Plack::Middleware=],
+    like $get->content, qr[Middleware needs a route definition and has to be wrapped in a sub.],
+      "the error message explains the need for a route def and a code ref";
+}
+
+sub plain_middleware_with_route {
+    @dispatch = ( '' => bless {}, "Plack::Middleware" );
+
+    my $get = run_request( GET => 'http://localhost/' );
+
+    cmp_ok $get->code, '==', 500, "a route definition consisting of a string and a plain middleware causes a bail";
+    like $get->content, qr[Middleware needs to be wrapped in a sub.],
       "the error message mentions the middleware class";
 }