Dancer icon indicating copy to clipboard operation
Dancer copied to clipboard

Add an on_hook_exception handler that I can hook

Open DrHyde opened this issue 10 years ago • 1 comments

I have a 'before' hook that checks that a user is asking for objects that belong to himself, and on failure calls status() and halt() to send appropriate errors back. For consistency with the rest of my application I would like to instead throw an exception. Exceptions from hooks aren't caught by 'on_route_exception', and if I try to hook 'on_handler_exception' or 'before_error_init' Dancer appears to get terribly confused and go into an infinite loop.

I suggest that either 'on_route_exception' handlers should be called if hooks throw exceptions, or there should be a new 'on_hook_exception' handler.

And if someone can point me at where I should start patching, I'll do it myself.

DrHyde avatar Apr 14 '14 16:04 DrHyde

on hook before_error_render, reset the $self->{exception} = undef; and it should work.

hook 'before_error_render' => sub {
    my $self = shift;
    my $exception = $self->exception;

    $self->{code} = $exception->http_code;
    $self->{message} = ...
    $self->{exception} = undef; # reset
};

fayland avatar Nov 19 '14 01:11 fayland