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??? };

In reply to How can I have a Dancer2 before hook return an HTTP error? by lembark

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.