[Catalyst] [again] die() in an action chain : does not break the chain !

Dami Laurent (PJ) laurent.dami at justice.ge.ch
Fri Mar 14 07:54:07 GMT 2008


Hello list,

Sorry, I already asked the same question a couple of days ago, but
probably I was not clear enough. 

My problem is that if there is an exception at the entry point of an
action chain, processing nevertheless continues until the endpoint of
the chain.

Consider for example the following action chain :

   sub one : Chained("/") :PathPart("one") :CaptureArgs(0) {
     my ($self, $c) = @_;
     $c->{stash}{msg} = "start_work";
     die "die in one";
   }

   sub two : Chained("one") :PathPart("two") :CaptureArgs(0) {
     my ($self, $c) = @_;
     $c->{stash}{msg} .= ", ". $self->expensive_work($c);
     die "die in two";
   }

   sub three : Chained("two") :PathPart("three") :Args(0) {
     my ($self, $c) = @_;
     $c->{stash}{msg} .= ", ". $self->expensive_work($c) . " again";
     die "die in three";
     $c->response->body($c->stash->{msg});
   }

   sub expensive_work {
     my ($self, $c) = @_;
     return "this took me a long time";
   }  

and the following finalize_error function :

sub finalize_error {
  my ($c) = @_;
  my $body = "got errors : " . join ("\n", @{$c->error})
           . "\nand message was :\n" . $c->stash->{msg};
  $c->response->body($body);
}

This yields

  got errors : Caught exception in
Cata_exceptions::Controller::Root->one "die in one at
D:/Temp/Cata_exceptions/scripts/../lib/Cata_exceptions/Controller/Root.p
m line 22."
  Caught exception in Cata_exceptions::Controller::Root->two "die in two
at
D:/Temp/Cata_exceptions/scripts/../lib/Cata_exceptions/Controller/Root.p
m line 28."
  Caught exception in Cata_exceptions::Controller::Root->three "die in
three at
D:/Temp/Cata_exceptions/scripts/../lib/Cata_exceptions/Controller/Root.p
m line 35."
  and message was :
  start_work, this took me a long time, this took me a long time again


Now the question is : how to avoid calling expensive_work() ? Is there a
way to tune up the chaining mechanism so that it would automatically
detach() at the first exception ?

Thanks in advance, Laurent Dami



More information about the Catalyst mailing list