[dancer-users] Triggering 404 error from a hook

Hermann Calabria hermann at ivouch.com
Tue Nov 10 01:35:46 GMT 2015


Hi all,

My application uses different databases depending on the client, which is always the first parameter in each route.  Example:

/:client/home
/:client/list
...

I’m using the following ‘before’ hook to set the database handle dbh:

# Figure out client, if any
my $client = param('client') || '';
unless ($client =~ /^[a-z0-9]{2,20}$/) {
  $client = ''; }

# If $client, get client dbh
if ($client) {
  if (!vars->{'dbh'}) {
    eval {
      var dbh => database({
        driver => "mysql",
        host => "localhost",
        dbi_params => ...
        database => "s_".$client,
        ...
        }); 
      }
    }
  if (!vars->{'dbh'}) {
    warning “Could not open client database”;   # Warning is present
    pass;   # Internal Server Error thrown
    } 
  }

I’d like Dancer to gracefully fall through to a 404 error in case s1_$client is not found or cannot be opened.  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.

Any ideas on how to implement this without having to repeat this code over and over and over within each route?

I’m also open to other ideas/approaches to implement this general scheme as long as I get a separate database for each :client (i.e., “create one huge database” is not a valid solution given the application requirements).

Thanks in advance for any thoughts!

Hermann
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20151109/7992ffdd/attachment.html>


More information about the dancer-users mailing list