| 1 | package Catalyst::ActionRole::RenderErrors; | ||
| 2 | |||
| 3 | use Moose::Role; | ||
| 4 | |||
| 5 | my $dont_dispatch_error = sub { | ||
| 6 | my ($self, $controller, $c) = @_; | ||
| 7 | return 1 if $c->req->method eq 'HEAD'; | ||
| 8 | return 1 if defined $c->response->body; | ||
| 9 | return 1 if $c->response->status =~ /^(?:204|3\d\d)$/; | ||
| 10 | return 0; | ||
| 11 | }; | ||
| 12 | |||
| 13 | around 'execute', sub { | ||
| 14 | my ($orig, $self, $controller, $c, @args) = @_; | ||
| 15 | my $ret = $self->$orig($controller, $c, @args); | ||
| 16 | |||
| 17 | return $ret if $self->$dont_dispatch_error($controller, $c); | ||
| 18 | |||
| 19 | my @errors = @{$c->error}; | ||
| 20 | return $ret unless @errors; | ||
| 21 | |||
| 22 | my $first = $errors[-1]; # We can only handle the last error in the stack | ||
| 23 | |||
| 24 | if($c->looks_like_http_error_obj($first)) { | ||
| 25 | my ($status_code, $additional_headers, $template_args) = $first->as_http_response; | ||
| 26 | $c->clear_errors && $c->dispatch_error($status_code, $additional_headers, $template_args) | ||
| 27 | unless ($c->debug && ($status_code >= 500)); | ||
| 28 | } else { | ||
| 29 | $c->log->error($first); | ||
| 30 | $c->clear_errors && $c->dispatch_error(500) unless $c->debug; | ||
| 31 | } | ||
| 32 | |||
| 33 | return $ret; | ||
| 34 | }; | ||
| 35 | |||
| 36 | 1; | ||
| 37 | |||
| 38 | =head1 NAME | ||
| 39 | |||
| 40 | Catalyst::ActionRole::RenderErrors - Automatically return an error page | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | package Example::Controller::Root; | ||
| 45 | |||
| 46 | use Moose; | ||
| 47 | use MooseX::MethodAttributes; | ||
| 48 | |||
| 49 | extends 'Catalyst::Controller'; | ||
| 50 | |||
| 51 | sub root :Chained(/) PathPart('') CaptureArgs(0) {} | ||
| 52 | |||
| 53 | sub not_found :Chained(root) PathPart('') Args { | ||
| 54 | my ($self, $c, @args) = @_; | ||
| 55 | $c->detach_error(404); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub die :Chained(root) PathPart(die) Args(0) { | ||
| 59 | die "saefdsdfsfs"; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub end :Does(RenderErrors) { } | ||
| 63 | |||
| 64 | __PACKAGE__->config(namespace=>''); | ||
| 65 | __PACKAGE__->meta->make_immutable; | ||
| 66 | |||
| 67 | =head1 DESCRIPTION | ||
| 68 | |||
| 69 | Tries to convert the last error in '$c->error' to something we can dispatch an error view too. | ||
| 70 | |||
| 71 | If the first error in '$c->error' is an object that looks like it does L<CatalystX::Utils::DoesHttpException> | ||
| 72 | then we use that error to get the HTTP status code and any additional arguments. If its not then we just return | ||
| 73 | a simple HTTP 500 Bad request. In that case we won't return any information in C<$c->error> since that might leak | ||
| 74 | sensitiver Perl debugging info. A stringified version of the error is sent to the error log. | ||
| 75 | |||
| 76 | Useful for API work since the default L<Catalyst> error page is in HTML and if your client is requesting | ||
| 77 | JSON we'll return a properly formatted response in C<application/json>. | ||
| 78 | |||
| 79 | B<NOTE> if you are in CATALYST_DEBUG mode then all HTTP 500 errors and non specific errors will still | ||
| 80 | dump the development debugging error screen. | ||
| 81 | |||
| 82 | =head1 SEE ALSO | ||
| 83 | |||
| 84 | L<CatalystX::Errors>. | ||
| 85 | |||
| 86 | =head1 AUTHOR | ||
| 87 | |||
| 88 | L<CatalystX::Errors>. | ||
| 89 | |||
| 90 | =head1 COPYRIGHT & LICENSE | ||
| 91 | |||
| 92 | L<CatalystX::Errors>. | ||
| 93 | |||
| 94 | =cut |