[Catalyst] "or die" replacement?

Phil Mitchell phil at 2people.org
Thu Mar 16 21:59:10 CET 2006


On 3/15/06, Andreas Jellinghaus <aj at ciphirelabs.com> wrote:
>
>
> What are your recommendations, how should I replace
> my "or die" code? It is important to me to give
> some feedback to the user that something went wrong,
> and - to be sure - create a proper entry in some
> log file, e.g. apache error log (catalyst running
> with mod_perl inside apache).


It's a good question, and I'd like to see more examples of what people do,
and maybe a best practice. Instead of die, my app throws named exceptions
(cf. Exception::Class), which are easy to catch, and you can provide
appropriate error msgs to the user based on what type of exception you
caught.

Every one of my controller actions looks basically like this:

sub foo : Local {
   eval {
       ... do all the foo stuff, and throw exceptions where necessary
};
if ($@) {
   my $err = $@;
   if ($err->isa(MyApp::Exception::File) {
       $msg = "Could not open that file";
   }
    elsif ($err->isa(MyApp::Exception::Number) {
       ...
   }
  ... etc.
}

Of course there are other ways to return error values, and it's a judgement
call which way to choose -- but in places where you're inclined to call
"die", this works very nicely.

--phil

Regards, Andreas
>
> _______________________________________________
> Catalyst mailing list
> Catalyst at lists.rawmode.org
> http://lists.rawmode.org/mailman/listinfo/catalyst
>



--
==========================
http://www.2people.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rawmode.org/pipermail/catalyst/attachments/20060316/9d907294/attachment.htm 


More information about the Catalyst mailing list