Skip to content

Commit 0a721e7

Browse files
committed
use try catch instead of eval, add method message exception
1 parent c734c15 commit 0a721e7

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

dist.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ author = Frederic Lechauve
33
license = None
44
copyright_holder = Weborama
55
copyright_year = 2014
6-
version = 0.07
6+
version = 0.08
77
main_module = lib/Dancer/Plugin/TimeoutManager.pm
88

99
[@Basic]
1010
[OurPkgVersion]
1111
[Prereqs] ; additional requirements
1212
Dancer = 1.3120
1313
List::MoreUtils = 0.33
14+
Try::Tiny = 0.11
1415

1516
[PruneFiles]
1617
files = ignore.txt

lib/Dancer/Plugin/TimeoutManager.pm

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use strict;
44
use warnings;
55
# VERSION
66

7+
use Try::Tiny;
78
use Dancer ':syntax';
89
use Dancer::Exception ':all';
910
use Dancer::Plugin;
@@ -28,6 +29,24 @@ register_exception ('InvalidMethod',
2829

2930
my @authorized_methods = ('get', 'post', 'put', 'delete');
3031

32+
=method exception_message
33+
34+
return the exception message
35+
This method can be used to catch the exception if the code used already contained a try catch
36+
37+
=cut
38+
39+
sub exception_message{
40+
41+
return 'Route Timeout Detected';
42+
}
43+
44+
=method timeout
45+
46+
Method that manage the timeout on a dancer request
47+
48+
=cut
49+
3150
sub timeout {
3251
my ($timeout,$method, $pattern, @rest);
3352
if (scalar(@_) == 4){
@@ -48,27 +67,33 @@ sub timeout {
4867
raise InvalidMethod => $method;
4968
}
5069

70+
my $exception_message = exception_message();
5171
my $timeout_route = sub {
5272
my $response;
5373

5474
#if timeout is not defined but a value is set in the headers for timeout
5575
$timeout = vars->{header_timeout} if (!defined $timeout && defined vars->{header_timeout});
5676

5777
# if timeout is not defined or equal 0 the timeout manager is not used
78+
my $timeout_exception;
5879
if (!$timeout){
5980
$response = $code->();
6081
}
6182
else{
62-
eval {
63-
local $SIG{ALRM} = sub { croak ("Route Timeout Detected"); };
83+
try {
84+
local $SIG{ALRM} = sub { croak ($exception_message); };
6485
alarm($timeout);
86+
6587
$response = $code->();
6688
alarm(0);
89+
}
90+
catch{
91+
$timeout_exception = $_;
6792
};
6893
alarm(0);
6994
}
7095
# Timeout detected
71-
if ($@ && $@ =~ /Route Timeout Detected/){
96+
if ($timeout_exception && $timeout_exception =~ /$exception_message/){
7297
my $response_with_timeout = Dancer::Response->new(
7398
status => 408,
7499
content => "Request Timeout : more than $timeout seconds elapsed."
@@ -135,6 +160,9 @@ If the timeout is set to 0, the behavior is the same than without any timeout de
135160
It's also possible to define route handlers that will set a per-request timeout protection, depending
136161
on the value of the header C<X-Dancer-Timeout>.
137162
163+
If your Dancer code already use try catch, the exeption may be catched.
164+
So exception_message method can be used to cath the content of the exception in your Dancer code.
165+
138166
=head1 AUTHOR
139167
140168
Frederic Lechauve, E<lt>frederic_lechauve at yahoo.frE<gt>

0 commit comments

Comments
 (0)