fixed action signature error handling
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declare / Action / CatchValidationError.pm
index f0c722a..f7e1f62 100644 (file)
@@ -6,19 +6,35 @@ role CatalystX::Declare::Action::CatchValidationError {
 
     around execute (Object $controller, Object $ctx, @rest) {
 
+        my $tc = $controller->meta->find_method_type_constraint($self->name)
+              || do {
+                   my $method = $controller->meta->find_method_by_name($self->name);
+                   ( $_ = $method->can('type_constraint') )
+                     ? $method->$_
+                     : undef
+                 };
+
+        if ($tc and my $error = $tc->validate([$controller, $ctx, @rest])) {
+
+            if ($ctx->debug) {
+                $ctx->error("BAD REQUEST: $error");
+            }
+            else {
+                $ctx->response->body( 'Bad Request' );
+                $ctx->response->status( 400 );
+            }
+            
+            $ctx->detach;
+        }
+
         try {
             $self->$orig($controller, $ctx, @rest);
         }
-        catch (Str $error where { /^Validation failed for/ }) {
+        catch (Any $error) {
 
-            $ctx->response->body( 'Bad Request' );
-            $ctx->response->status( 400 );
+            $ctx->error($error);
             $ctx->detach;
         }
-        catch (Any $other) {
-
-            die $other;
-        }
 
         return 1;
     }