changed type validation error to 404
[catagits/CatalystX-Declare.git] / lib / CatalystX / Declare / Action / CatchValidationError.pm
CommitLineData
5fb5cef1 1use MooseX::Declare;
2
3role CatalystX::Declare::Action::CatchValidationError {
4
5 use TryCatch;
6
7 around execute (Object $controller, Object $ctx, @rest) {
8
392e5076 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 {
67f39940 23 $ctx->response->body( 'Not found' );
24 $ctx->response->status( 404 );
392e5076 25 }
26
27 $ctx->detach;
28 }
29
5fb5cef1 30 try {
31 $self->$orig($controller, $ctx, @rest);
32 }
392e5076 33 catch (Any $error) {
5fb5cef1 34
392e5076 35 $ctx->error($error);
5fb5cef1 36 $ctx->detach;
37 }
5fb5cef1 38
39 return 1;
40 }
41}