Actually, you're on the right way. But instead of calling render in
controller (you'll get many duplicates of the same code), you better
just leave only "die" there, and call render method from the hook.
Smthng like this:
# hook (you can also consider arount_action hook)
$self->hook(around_dispatch => sub ($next, $c) {
eval { $next->(); 1 } or $c->render('bad/value', msg => $@);
});
# in controller
sub myaction($c) {
die MyException->new() unless $c->stash('positive') > 0;
...
}
On 12/01/2016 07:52 PM, Peter Valdemar Mørch wrote:
Say in my controller I'm 5 levels deep and discover there is a problem
with the user's input. I call:
$c->render(
template => 'errors/badValues',
message => 'The value of foo cannot be "bar"',
);
And now I want to stop the entire request right there. I guess the
behaviour I'm looking for is that of an exception - return up the call
stack - without having to do something with a return value and then
callSub()
or return;
at every stack level. So what I've done is create an exception type and
$c->render(
template => 'errors/badValues',
message => 'The value of foo cannot be "bar"',
);
die(StopRenderingException->new());
And then in setup:
$self->hook( around_dispatch => sub {
my ( $next, $c ) = @_;
eval {
$next->();
};
if ($@) {
my $err = $@;
if (blessed($err) && $err->isa('StopRenderingException')) {
return;
} else {
die $err;
}
}
});
It does have a hacky smell to it though. Is there a better best-practice?
Peter
--
You received this message because you are subscribed to the Google
Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to mojolicious+unsubscr...@googlegroups.com
<mailto:mojolicious+unsubscr...@googlegroups.com>.
To post to this group, send email to mojolicious@googlegroups.com
<mailto:mojolicious@googlegroups.com>.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to mojolicious+unsubscr...@googlegroups.com.
To post to this group, send email to mojolicious@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.