PerlDancer / PerlDancer/Dancer2

Exception in before request hook does not allow send_error in route exception hook

Open
#1,109 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Question
Dominant language
Perl
Stars
604
Forks
288
Avg merge
1d 5h
Merged PRs (30d)
5

Description

I'm doing some validation in the before request hook, and would like any exception that occurs in validation to be caught normally (utilize send_error) by route exception hook. I can't seem to make it happy.

hook before => sub {
    die "We have an exception";
    # send_error("I can do it here fine");  # OK
};

hook on_route_exception => sub {
    my ($app, $error) = @_;
    send_error("Oops", 500);
};

will produce

[ABC::App:28018] core @2016-01-19 22:41:33> Entering hook core.app.before_request in (eval 61) l. 1
[ABC::App:28018] error @2016-01-19 22:41:33> Exception caught in 'core.app.before_request' filter: Hook error: We have an exception at /home/mike/dev/perl/ABC-App/bin/../lib/ABC/App.pm line 8.
 at /home/mike/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Dancer2/Core/App.pm line 971. in /home/mike/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Dancer2/Core/App.pm l. 526
[ABC::App:28018] error @2016-01-19 22:41:33> Route exception: Exception caught in 'core.app.before_request' filter: Hook error: We have an exception at /home/mike/dev/perl/ABC-App/bin/../lib/ABC/App.pm line 8.
 at /home/mike/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Dancer2/Core/App.pm line 971.
 at /home/mike/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Dancer2/Core/Role/Hookable.pm line 115. in /home/mike/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Dancer2/Core/App.pm l. 1288
[ABC::App:28018] core @2016-01-19 22:41:33> Entering hook core.app.route_exception in (eval 61) l. 1
[ABC::App:28018] error @2016-01-19 22:41:33> Exception caught in 'core.app.route_exception' filter: Hook error: Function 'send_error' must be called from a route handler at /home/mike/dev/perl/ABC-App/bin/../lib/ABC/App.pm line 13.
 at /home/mike/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Dancer2/Core/App.pm line 971. in /home/mike/perl5/perlbrew/perls/perl-5.22.1/lib/site_perl/5.22.1/Dancer2/Core/App.pm l. 526

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Dancer2/Core/App.pm around the before-request and route-exception handling shown in the trace, then inspect Dancer2/Core/Role/Hookable.pm around line 115. Reproduce the example and verify that an exception from before_request reaches on_route_exception without send_error failing.

Written by the indexing model from the issue text.

Assessment

Tech stack
perl
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.