| 1 | # $Id: MortalForward.pm 1111 2006-02-22 00:39:49Z ykerherve $ | ||
| 2 | package Catalyst::Plugin::MortalForward; | ||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | our $VERSION = '0.01'; | ||
| 7 | |||
| 8 | use NEXT; | ||
| 9 | |||
| 10 | sub execute { | ||
| 11 | my $c = shift; | ||
| 12 | my ($class, $action) = @_; | ||
| 13 | local $NEXT::NEXT{ $c, 'execute' }; | ||
| 14 | my $error_count = scalar @{$c->error}; | ||
| 15 | $c->NEXT::execute(@_); | ||
| 16 | # Don't die for internal actions | ||
| 17 | return $c->state if $c->depth < 3 || $action->name =~ m/^_/; | ||
| 18 | |||
| 19 | my @errors = @{ $c->error }; | ||
| 20 | if ($error_count < scalar @errors) { | ||
| 21 | my $error = pop @errors; | ||
| 22 | $c->error(0); | ||
| 23 | $c->error(@errors); | ||
| 24 | die $error; | ||
| 25 | } | ||
| 26 | return $c->state; | ||
| 27 | } | ||
| 28 | |||
| 29 | "Il ne doit en rester qu'un"; | ||
| 30 | |||
| 31 | =pod | ||
| 32 | |||
| 33 | =head1 NAME | ||
| 34 | |||
| 35 | Catalyst::Plugin::MortalForward - Make forward() to throw exception | ||
| 36 | |||
| 37 | =head1 SYNOPSIS | ||
| 38 | |||
| 39 | use Catalyst qw( MortalForward ); | ||
| 40 | |||
| 41 | sub someaction : Local { | ||
| 42 | ... | ||
| 43 | $c->forward('check_input'); # may die | ||
| 44 | |||
| 45 | # never executed if forward dies | ||
| 46 | do_something_important(); # assume that the input has been checked | ||
| 47 | } | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | I<Catalyst::Plugin::MortalForward> is a small plugin that changes the behaviour | ||
| 52 | of C<< $c->forward >> which usually never dies (because the forwarded code | ||
| 53 | is internally wrapped into an eval block. | ||
| 54 | |||
| 55 | This plugin changes this behaviour B<globally>. The forward method will throw | ||
| 56 | exceptions (that you should be carefull to handle at somepoint or the default | ||
| 57 | Catalyst error page will be displayed) | ||
| 58 | |||
| 59 | =head1 SEE ALSO | ||
| 60 | |||
| 61 | Discussion on the Catalyst mailing list: | ||
| 62 | L<http://lists.rawmode.org/pipermail/catalyst/2006-January/004874.html> | ||
| 63 | (followed-up in February) | ||
| 64 | |||
| 65 | =head1 BUGS & TODOS | ||
| 66 | |||
| 67 | Please report any problem. | ||
| 68 | If you let the exception reach Catalyst internal, then the error (which is logged | ||
| 69 | might be a bit messy) because it holds the information of all successive layers | ||
| 70 | that the exception went thru. For instance : | ||
| 71 | |||
| 72 | Caught exception in TestApp->class_fwd "Caught exception in TestApp::C::Elsewhere->test "I die too, sorry at /Users/yann/Catalyst/Catalyst-Plugin-MortalForward/t/lib/TestApp/C/Elsewhere.pm line 8." at lib/Catalyst/Plugin/MortalForward.pm line 24." | ||
| 73 | |||
| 74 | |||
| 75 | =head1 AUTHOR | ||
| 76 | |||
| 77 | Six Apart, cpan@sixapart.com | ||
| 78 | |||
| 79 | =head1 LICENSE | ||
| 80 | |||
| 81 | I<Catalyst::Plugin::MortalForward> is free software; you may redistribute it | ||
| 82 | and/or modify it under the same terms as Perl itself. | ||
| 83 | |||
| 84 | =head1 AUTHOR & COPYRIGHT | ||
| 85 | |||
| 86 | Except where otherwise noted, I<Catalyst::Plugin::MortalForward> is | ||
| 87 | Copyright 2006 Six Apart, cpan@sixapart.com. All rights reserved. | ||
| 88 | |||
| 89 | =cut |