Skip to content

Commit e4958ca

Browse files
committed
Merge branch 'feature/on-hook-exception-source'
2 parents b07a30d + 8d2eb7d commit e4958ca

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Karen Etheridge, Damien Krotkine, Yanick Champoux)
1818
* GH #1737: Add on_hook_exception for errors during hook processing
1919
(Andy Beverley)
20+
* PR #1739: Add source to on_hook_exception (Andy Beverley)
2021

2122
[ DOCUMENTATION ]
2223
* GH #1342: Document skipping private methods in pod coverage tests

lib/Dancer2/Core/App.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,7 +1242,7 @@ sub compile_hooks {
12421242
# Don't execute the hook_exception hook if the exception
12431243
# has been generated from a hook exception handler itself,
12441244
# thus preventing potentially recursive code.
1245-
$app->execute_hook( 'core.app.hook_exception', $app, $err )
1245+
$app->execute_hook( 'core.app.hook_exception', $app, $err, $position )
12461246
unless $is_hook_exception;
12471247
my $is_halted = $app->response->is_halted; # Capture before cleanup
12481248
# We can't cleanup if we're in the hook for a hook

lib/Dancer2/Manual.pod

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -928,16 +928,19 @@ L<Dancer2::Core::App> and the error as arguments.
928928
};
929929

930930
C<on_hook_exception> is called when an exception has been caught within a hook,
931-
just before logging and rethrowing it higher. This hook receives a
932-
L<Dancer2::Core::App> and the error as arguments. If the function provided to
933-
on_hook_exception causes an exception itself, then this will be ignored (thus
934-
preventing a potentially recursive situation). However, it is still possible
935-
for the function to set a custom response and halt it (and optionally die),
936-
thus providing a method to render custom content in the event of an exception
937-
in a hook. This is shown in the example below:
931+
just before logging and rethrowing it higher. This hook receives as arguments:
932+
L<Dancer2::Core::App>, the error string and the name of the hook where the
933+
exception occurred. The hook name is the full hook name (e.g.
934+
C<core.app.route_exception>).
935+
936+
If the function provided to on_hook_exception causes an exception itself, then
937+
this will be ignored (thus preventing a potentially recursive situation).
938+
However, it is still possible for the function to set a custom response and
939+
halt it (and optionally die), thus providing a method to render custom content
940+
in the event of an exception in a hook. This is shown in the example below:
938941

939942
hook on_hook_exception => sub {
940-
my ($app, $error) = @_;
943+
my ($app, $error, $hook_name) = @_;
941944
$app->response->content("Some custom content for the response");
942945
$app->response->halt;
943946
};

t/hooks.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,10 @@ my $tests_flags = {};
169169
};
170170

171171
hook on_hook_exception => sub {
172-
my ($app, $error) = @_;
172+
my ($app, $error, $hook_name) = @_;
173173
::is ref($app), 'Dancer2::Core::App';
174174
::like $error, qr/this is an exception in the after hook/;
175+
::is $hook_name, 'core.app.after_request';
175176
$app->response->content("Setting response from exception hook");
176177
$app->response->halt;
177178
};

0 commit comments

Comments
 (0)