[dancer-users] Triggering 404 error from a hook
Warren Young
wyml at etr-usa.com
Tue Nov 10 05:57:51 GMT 2015
On Nov 9, 2015, at 6:35 PM, Hermann Calabria <hermann at ivouch.com> wrote:
>
> # Figure out client, if any
> my $client = param('client') || '';
> unless ($client =~ /^[a-z0-9]{2,20}$/) {
> $client = ''; }
>
> # If $client, get client dbh
> if ($client) {
Two lines replaces all that:
my $client = param('client') || ‘';
if ($client =~ /^[a-z0-9]{2,20}$/) {
> eval {
> var dbh => database({
> driver => "mysql",
> host => "localhost",
> dbi_params => ...
> database => "s_".$client,
> ...
> });
> }
You should be caching database connections. Re-opening it on each and every hit is a bit expensive, even for a localhost connection.
> I’d like Dancer to gracefully fall through to a 404 error
Why 404? That means the URI names a nonexistent resource, which is more appropriate for something like SELECT … = 0 records. This feels more like a 500 error.
Unless, that is, “missing customer DB” is something you expect your users to run into.
> I’ve tried “pass” and “forward”, but I receive a cryptic “Internal Server Error” which I assume is because these keywords are not valid within hooks, but I’m not sure.
Try send_error() instead. If it works, it’s more sensible anyway.
More information about the dancer-users
mailing list