[dancer-users] Triggering 404 error from a hook

Warren Young wyml at etr-usa.com
Tue Nov 10 20:05:58 GMT 2015

On Nov 9, 2015, at 11:16 PM, Hermann Calabria <hermann at ivouch.com> wrote:
> It seems maybe send_error() is not allowed within a before_hook.

We use redirect() for similar purposes here on D1, and it works.  Maybe that will suffice for you.

>> You should be caching database connections.
> Hmmm does this code not do that?  I'm using the Dancer Database plugin (and not the old plain DBI),

I had no idea where database() came from when I wrote that, and I don’t use that plugin anyway.  If you force it to use a TCP connection by giving a host name, you can probably find out if it creates a new connection for each page load with:

   netstat -na | grep 3306.*ESTA | wc -l


   sudo lsof -n | grep mysql.sock | wc -l

(The first for TCP, the second for the Unix domain socket.)

>> 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.

Redirecting to a login or home page is probably a more common response to that sort of error, since a 404 page doesn’t help the user fix the problem.

404 would be appropriate if a logged-in user tried to go to /:client/foo/qux, for example: i.e. a clear case of a dead bookmark, or bogus hand-hacking of the URL.

More information about the dancer-users mailing list