Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it under the |
6 |
# terms of the GNU General Public License as published by the Free Software |
7 |
# Foundation; either version 3 of the License, or (at your option) any later |
8 |
# version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
13 |
# |
14 |
# You should have received a copy of the GNU General Public License along |
15 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
16 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 1; |
21 |
use Test::Mojo; |
22 |
use Test::Warn; |
23 |
|
24 |
use t::lib::Mocks; |
25 |
use Koha::Exceptions; |
26 |
|
27 |
use Log::Log4perl; |
28 |
use Mojolicious::Lite; |
29 |
use Try::Tiny; |
30 |
|
31 |
my $config = { |
32 |
'log4perl.logger.rest.Koha.REST.V1' => 'ERROR, TEST', |
33 |
'log4perl.appender.TEST' => 'Log::Log4perl::Appender::TestBuffer', |
34 |
'log4perl.appender.TEST.layout' => 'SimpleLayout', |
35 |
}; |
36 |
t::lib::Mocks::mock_config('log4perl_conf', $config); |
37 |
|
38 |
my $remote_address = '127.0.0.1'; |
39 |
my $t = Test::Mojo->new('Koha::REST::V1'); |
40 |
|
41 |
subtest 'default_exception_handling() tests' => sub { |
42 |
plan tests => 5; |
43 |
|
44 |
add_default_exception_routes($t); |
45 |
|
46 |
my $appender = Log::Log4perl->appenders->{TEST}; |
47 |
|
48 |
subtest 'Mojo::Exception' => sub { |
49 |
plan tests => 4; |
50 |
|
51 |
$t->get_ok('/default_exception_handling/mojo') |
52 |
->status_is(500) |
53 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
54 |
|
55 |
like($appender->buffer, qr/ERROR - test mojo exception/, |
56 |
'Found test mojo exception in log'); |
57 |
$appender->{appender}->{buffer} = undef; |
58 |
}; |
59 |
|
60 |
subtest 'die() outside try { } catch { };' => sub { |
61 |
plan tests => 4; |
62 |
|
63 |
$t->get_ok('/default_exception_handling/dieoutsidetrycatch') |
64 |
->status_is(500) |
65 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
66 |
like($appender->buffer, qr/ERROR - die outside try-catch/, |
67 |
'Found die outside try-catch in log'); |
68 |
$appender->{appender}->{buffer} = undef; |
69 |
}; |
70 |
|
71 |
subtest 'DBIx::Class::Exception' => sub { |
72 |
plan tests => 4; |
73 |
|
74 |
$t->get_ok('/default_exception_handling/dbix') |
75 |
->status_is(500) |
76 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
77 |
like($appender->buffer, qr/ERROR - DBIx::Class::Exception => .* test dbix exception/, |
78 |
'Found test dbix exception in log'); |
79 |
$appender->{appender}->{buffer} = undef; |
80 |
}; |
81 |
|
82 |
subtest 'Koha::Exceptions::Exception' => sub { |
83 |
plan tests => 4; |
84 |
|
85 |
$t->get_ok('/default_exception_handling/koha') |
86 |
->status_is(500) |
87 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
88 |
like($appender->buffer, qr/ERROR - Koha::Exceptions::Exception => test koha exception/, |
89 |
'Found test koha exception in log'); |
90 |
$appender->{appender}->{buffer} = undef; |
91 |
}; |
92 |
|
93 |
subtest 'Unknown exception' => sub { |
94 |
plan tests => 4; |
95 |
|
96 |
$t->get_ok('/default_exception_handling/unknown') |
97 |
->status_is(500) |
98 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
99 |
like($appender->buffer, qr/ERROR - Unknown::Exception::OhNo => \{"what":"test unknown exception"\}/, |
100 |
'Found test unknown exception in log'); |
101 |
$appender->{appender}->{buffer} = undef; |
102 |
}; |
103 |
}; |
104 |
|
105 |
sub add_default_exception_routes { |
106 |
my ($t) = @_; |
107 |
|
108 |
# Mojo::Exception |
109 |
$t->app->routes->get('/default_exception_handling/mojo' => sub { |
110 |
try { |
111 |
die "test mojo exception"; |
112 |
} catch { |
113 |
Koha::Exceptions::rethrow_exception($_); |
114 |
}; |
115 |
}); |
116 |
|
117 |
# die outside try-catch |
118 |
$t->app->routes->get('/default_exception_handling/dieoutsidetrycatch' => sub { |
119 |
die "die outside try-catch"; |
120 |
}); |
121 |
|
122 |
# DBIx::Class::Exception |
123 |
$t->app->routes->get('/default_exception_handling/dbix' => sub { |
124 |
package Koha::REST::V1::Test; |
125 |
try { |
126 |
DBIx::Class::Exception->throw('test dbix exception'); |
127 |
} catch { |
128 |
Koha::Exceptions::rethrow_exception($_); |
129 |
}; |
130 |
}); |
131 |
|
132 |
# Koha::Exceptions::Exception |
133 |
$t->app->routes->get('/default_exception_handling/koha' => sub { |
134 |
try { |
135 |
Koha::Exceptions::Exception->throw('test koha exception'); |
136 |
} catch { |
137 |
Koha::Exceptions::rethrow_exception($_); |
138 |
}; |
139 |
}); |
140 |
|
141 |
# Unknown exception |
142 |
$t->app->routes->get('/default_exception_handling/unknown' => sub { |
143 |
try { |
144 |
my $exception = { what => 'test unknown exception'}; |
145 |
bless $exception, 'Unknown::Exception::OhNo'; |
146 |
die $exception; |
147 |
} catch { |
148 |
Koha::Exceptions::rethrow_exception($_); |
149 |
}; |
150 |
}); |
151 |
} |
152 |
|
153 |
1; |