in reply to Re^3: Dancer2 hook
in thread Dancer2 hook

Correct. Here you go:
package Dancer2::Plugin::TestHook use Dancer2; use Dancer2::Core::Error; hook after_error => sub { my $content = shift; $content->{content} .= "hello world"; } 1;
This hook works fine, but it doesn't get called when I use the module (Dancer::Plugin::TestHook) somewhere else. That's the problem.

Replies are listed 'Best First'.
Re^5: Dancer2 hook
by Anonymous Monk on Aug 13, 2019 at 01:04 UTC

    Still incomplete.

    Where is the app that triggers an error?

      package Howdy; use Dancer2; use Dancer2::Plugin::TestHook; get '/' => sub { template 'index' => { 'title' => 'Howdy' }; }; get '/hello' => sub { die "We are out of ponies\n"; return "Welcome to Dancer2 Web Application"; }; true;

        Hi

        That doesn't compile

        Also, it is still incomplete

        Still, if I go ahead and fill in the blanks by copying  dancer2 -a  HookTest I think I see what you see

        You should read https://metacpan.org/pod/Dancer2::Plugin#Writing-the-plugin

        This triggers

        package TestHook; use Dancer2::Plugin; sub BUILD { my( $plugin ) = @_; $plugin->app->add_route( method => 'get', regexp => '/die', code => sub { die "get '/die' => sub { " }, ); $plugin->app->add_route( method => 'get', regexp => '/ride', code => sub { 'ride' }, ); $plugin->app->add_hook( Dancer2::Core::Hook->new( name => 'after_error', #~ code => sub { $_[0]->content( $_[0]->content . " ... please +?" ) } code => sub { my $content = shift; warn ' From TestHook From TestHook From TestHo +ok From TestHook From TestHook '; $content->{content} .= " hello world From TestHook + "; } )); } 1; __END__