[dancer-users] http_throw and Dancer2

Paul Clements paul.clements at us.sios.com
Fri Feb 5 13:23:21 EST 2021


On Thu, Feb 4, 2021 at 9:06 AM Andrew Beverley <andy at andybev.com> wrote:

> Examples also in the pod (although just noticed one isn't formatted
> properly):
>
> https://metacpan.org/pod/Dancer2::Plugin::LogReport#$obj-%3Efatal_handler()
>
> The idea is that you throw an exception anywhere:
>
>   error __x"Invalid email address: {email}", email => $email
>
> Then it ends up in the fatal_handler to return the custom response

error and fatal_handler seem to work much better than the http_throw
approach I had been using...

> > So, ideally, I'd rather not have anything grab and rethrow exceptions
> > as strings (as seems to happen now in a few places in the Dancer2
> > codebase). I guess I'm really looking for a way to avoid or circumvent
> > what seems to be the default Dancer exception handling behavior.
>
> If you use the plugin, the exceptions will all be objects. You could
> also add classes to define response codes. E.g.
>
>   error "You are forbidden to access this resource", _class => "forbidden"
>
> And then in the handler:
>
>   if $msg->inClass('forbidden') ...

Yes, the ability to overload the error object allows me to pass enough
information to get the error responses I need.

Thanks for your help,
Paul


More information about the dancer-users mailing list