<div dir="ltr"><div>Here's one module we're going to use in the future for all exceptions in Dancer2: Throwable::Error (and its role: Throwable).<br></div><div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 20, 2014 at 7:16 PM, Andrew Beverley <span dir="ltr"><<a href="mailto:andy@andybev.com" target="_blank">andy@andybev.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Thu, 2014-11-20 at 13:29 +0200, WK wrote:<br>
> So I have simple question: how and where to catch errors in Dancer<br>
> app?<br>
<br>
</span>As Ovid says, there are many ways to approach the problem. This is mine.<br>
<br>
I use Log::Report, which has the advantage of doing translations and<br>
logging, as well as handling exceptions. For errors, it provides various<br>
levels ("error", "warn", "info" etc).<br>
<br>
Log::Report provides one or more dispatchers, which can be configured to<br>
filter on the severity of the error message. A dispatcher can be a<br>
callback, so I configure multiple dispatchers for different types of<br>
messages.<br>
<br>
If it's a user error then that will be displayed to the web page, and<br>
depending on the severity I'll either allow the route to continue or<br>
stop it there. Otherwise I'll log it to the system, which I plan to do<br>
with errbit in the future.<br>
<br>
Whenever I call a function in my controller, I wrap it in my own eval<br>
equivalent:<br>
<br>
process(sub {...})<br>
<br>
This function does the work, and if it's fatal (including for a user<br>
error) it stops the route and displays the error.<br>
<br>
sub process<br>
{<br>
my $coderef = shift;<br>
try {&$coderef};<br>
$@->reportFatal(to => 'error_handler', is_fatal => 0);<br>
$@ ? 0 : 1; # Return true on success<br>
}<br>
<br>
In terms of exceptions that I wasn't expecting (e.g. unable to connect<br>
to the database), I catch all of these using the init_error hook:<br>
<br>
hook init_error => sub {<br>
my $error = shift;<br>
panic $error->{exception};<br>
};<br>
<br>
This works, but it's a bit messy, so I'd like to find a better way of<br>
doing it.<br>
<br>
That's it in short. I hope to write it all up at some point and/or<br>
create a plugin, but feel free to ask questions in the meantime.<br>
<br>
Andy<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
_______________________________________________<br>
dancer-users mailing list<br>
<a href="mailto:dancer-users@dancer.pm">dancer-users@dancer.pm</a><br>
<a href="http://lists.preshweb.co.uk/mailman/listinfo/dancer-users" target="_blank">http://lists.preshweb.co.uk/mailman/listinfo/dancer-users</a><br>
</div></div></blockquote></div><br></div>