lembark has asked for the wisdom of the Perl Monks concerning the following question:
Q: What can I use in a Dancer2 before hook to cause the request being processed to hand back an HTTP error?
Say I have a before hook that will filter out requests which are not worth processing. I would like to short-circuit the request and simply return, say, a 404. I've tried variations on bare return, returning a Dancer2::Core::Error object or the result of calling ->throw on one with no luck. I can abort with a 'die' but that leaves me returning a 500, not a 404 whatever. There is documentation in Dancer2 on handling errors generated by Dancer2 itself, but I cannot find anything that describes how to raise an error/exception/whatever from the Perl code running in handlers. The simplest example I can think of is a lightweight handler that rejects botched requests based on IP, username, etc, that are blacklisted.
hook before => sub { ... is_bogus_request $request and ... # what??? };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How can I have a Dancer2 before hook return an HTTP error?
by Arunbear (Prior) on Jan 25, 2025 at 13:12 UTC | |
|
Re: How can I have a Dancer2 before hook return an HTTP error?
by 1nickt (Canon) on Jan 26, 2025 at 12:37 UTC |