changed type validation error to 404
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declare / Action / CatchValidationError.pm
1 use MooseX::Declare;
2
3 role CatalystX::Declare::Action::CatchValidationError {
4
5     use TryCatch;
6
7     around execute (Object $controller, Object $ctx, @rest) {
8
9         my $tc = $controller->meta->find_method_type_constraint($self->name)
10               || do {
11                    my $method = $controller->meta->find_method_by_name($self->name);
12                    ( $_ = $method->can('type_constraint') )
13                      ? $method->$_
14                      : undef
15                  };
16
17         if ($tc and my $error = $tc->validate([$controller, $ctx, @rest])) {
18
19             if ($ctx->debug) {
20                 $ctx->error("BAD REQUEST: $error");
21             }
22             else {
23                 $ctx->response->body( 'Not found' );
24                 $ctx->response->status( 404 );
25             }
26             
27             $ctx->detach;
28         }
29
30         try {
31             $self->$orig($controller, $ctx, @rest);
32         }
33         catch (Any $error) {
34
35             $ctx->error($error);
36             $ctx->detach;
37         }
38
39         return 1;
40     }
41 }