<HTML><HEAD></HEAD>
<BODY dir=ltr>
<DIV dir=ltr>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: 'Calibri'; COLOR: #000000">
<DIV>Following this useful Advent about Dancer::Exception:</DIV>
<DIV> </DIV>
<DIV><A title=http://advent.perldancer.org/2011/6
href="http://advent.perldancer.org/2011/6">http://advent.perldancer.org/2011/6</A></DIV>
<DIV> </DIV>
<DIV>I am attempting to refactor exception handling in a large D1 app, using
Dancer::Exception. </DIV>
<DIV> </DIV>
<DIV>Ideally I’d like to Raise Friendly => ‘Some Friendly Error Message’ to
report user-friendly error messages without logging, and everything else
(whether raised by Dancer, die(), or by something else such as a database error)
should just report “Internal Server Error” and log it.</DIV>
<DIV> </DIV>
<DIV>Furthermore, I’d like to be able to use Raise Friendly => ‘Some Friendly
Error Message’ anywhere within the route code or within any package used by the
route (example below uses Some::Package).</DIV>
<DIV> </DIV>
<DIV>See code below. It *appears* to work for all shown cases (1, 2,
3a-3d), but I would really appreciate critique and recommendations. In
particular:</DIV>
<DIV> </DIV>
<DIV>1. I don’t want to repeat the same catch {} section within every single
route (following principles of D.R.Y.), so would really appreciate
recommendations for cleanup. I tried using hook before_error_init and
before_error_render but got nowhere fast (maybe I shouldn’t have given
up?)</DIV>
<DIV> </DIV>
<DIV>2. Am I using register_exception(‘Friendly’); correctly? I get a
compilation error if I attempt to register_exception(‘Friendly’); within
Some::Package. </DIV>
<DIV> </DIV>
<DIV>3. Following D.R.Y., it would be nice to not have to
register_exception(‘Friendly’) in every single route. I tried declaring it
within app.pl but got the expected compilation error.</DIV>
<DIV> </DIV>
<DIV>Thanks all,</DIV>
<DIV>Hermann</DIV>
<DIV> </DIV>
<DIV>---</DIV>
<DIV> </DIV>
<DIV>package mytest;</DIV>
<DIV>use strict;</DIV>
<DIV>use Dancer ':syntax';</DIV>
<DIV>use Dancer::Exception qw(:all);</DIV>
<DIV>register_exception('Friendly');</DIV>
<DIV>use lib "/home/s1/modules";</DIV>
<DIV>use Some::Package;</DIV>
<DIV> </DIV>
<DIV>get '/' => sub {</DIV>
<DIV> </DIV>
<DIV> try {</DIV>
<DIV> </DIV>
<DIV> # Case 1</DIV>
<DIV> Raise Friendly => ‘Friendly within mytest’;</DIV>
<DIV> </DIV>
<DIV> # Case 2</DIV>
<DIV> die ‘Unfriendly within my test’;</DIV>
<DIV> </DIV>
<DIV> # Case 3a-3d</DIV>
<DIV> &Some::Package::Test();</DIV>
<DIV> </DIV>
<DIV> }</DIV>
<DIV> catch {</DIV>
<DIV> my ($exception) = @_;</DIV>
<DIV> if (ref($exception) =~ /^Dancer\:\:Exception/) {</DIV>
<DIV> if ($exception->does('Friendly')) {</DIV>
<DIV> status 400;</DIV>
<DIV> return "got a friendly
exception: " . $exception->message;</DIV>
<DIV> }</DIV>
<DIV> warning 'got an unknown exception to
rethrow';</DIV>
<DIV> $exception->rethrow;</DIV>
<DIV> }</DIV>
<DIV> die $exception;</DIV>
<DIV> };</DIV>
<DIV>};</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>----</DIV>
<DIV> </DIV>
<DIV>package Some::Package;</DIV>
<DIV>use Dancer ':syntax';</DIV>
<DIV>use Dancer::Plugin::Database;</DIV>
<DIV>use Dancer::Exception qw(:all);</DIV>
<DIV>return –1;</DIV>
<DIV> </DIV>
<DIV>sub Test {</DIV>
<DIV> </DIV>
<DIV> # Case 3a: </DIV>
<DIV> raise Friendly => 'A friendly error within Some::Package’;</DIV>
<DIV> </DIV>
<DIV> # Case 3b:</DIV>
<DIV> die ‘Unfriendly within Some::Package’;</DIV>
<DIV> </DIV>
<DIV> # Case 3c: database error (unfriendly)</DIV>
<DIV> database(‘mydb’)->selectrow_array("SELbarfECT 1");</DIV>
<DIV> </DIV>
<DIV> # Case 3d: Some other package</DIV>
<DIV> &Some::Other::Package::Test();</DIV>
<DIV> </DIV>
<DIV>};</DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV>-----</DIV>
<DIV> </DIV>
<DIV>And for completeness, app.pl:</DIV>
<DIV> </DIV>
<DIV>use strict;</DIV>
<DIV>use Dancer;</DIV>
<DIV>use mytest;</DIV>
<DIV>dance;</DIV>
<DIV> </DIV>
<DIV> </DIV></DIV></DIV></BODY></HTML>