[dancer-users] Triggering 404 error from a hook

Hermann Calabria hermann at ivouch.com
Tue Nov 10 06:16:25 GMT 2015


Thanks for the response!

> Try send_error() instead.  If it works, it’s more sensible anyway.

It doesn't appear to work. Same error as 'pass' and 'forward'.  It seems 
maybe send_error() is not allowed within a before_hook.

> You should be caching database connections.  Re-opening it on each and 
> every hit is a bit expensive, even for a localhost connection.

Hmmm does this code not do that?  I'm using the Dancer Database plugin (and 
not the old plain DBI), just setting the database dynamically (based on 
$client) instead of from the typical ./config.yml.  I assumed each database 
would be cached automatically by the plugin.

> Why 404?

Because the routes are named
/:client
/:client/foo
/:client/bar
/:client/foo/bar/foobar
etc...

so, if a database for /:client doesn't exist, it means (within the context 
of my app) the URI doesn't exist.

-----Original Message----- 
From: Warren Young
Sent: Monday, November 09, 2015 9:57 PM
To: Perl Dancer users mailing list
Subject: Re: [dancer-users] Triggering 404 error from a hook

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.
_______________________________________________
dancer-users mailing list
dancer-users at dancer.pm
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users 



More information about the dancer-users mailing list