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 |
my $tx; |
41 |
|
42 |
subtest 'default_exception_handling() tests' => sub { |
43 |
plan tests => 5; |
44 |
|
45 |
add_default_exception_routes($t); |
46 |
|
47 |
my $appender = Log::Log4perl->appenders->{TEST}; |
48 |
|
49 |
subtest 'Mojo::Exception' => sub { |
50 |
plan tests => 4; |
51 |
|
52 |
$t->get_ok('/default_exception_handling/mojo') |
53 |
->status_is(500) |
54 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
55 |
|
56 |
like($appender->buffer, qr/ERROR - test mojo exception/, |
57 |
'Found test mojo exception in log'); |
58 |
$appender->{appender}->{buffer} = undef; |
59 |
}; |
60 |
|
61 |
subtest 'die() outside try { } catch { };' => sub { |
62 |
plan tests => 4; |
63 |
|
64 |
$t->get_ok('/default_exception_handling/dieoutsidetrycatch') |
65 |
->status_is(500) |
66 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
67 |
like($appender->buffer, qr/ERROR - die outside try-catch/, |
68 |
'Found die outside try-catch in log'); |
69 |
$appender->{appender}->{buffer} = undef; |
70 |
}; |
71 |
|
72 |
subtest 'DBIx::Class::Exception' => sub { |
73 |
plan tests => 4; |
74 |
|
75 |
$t->get_ok('/default_exception_handling/dbix') |
76 |
->status_is(500) |
77 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
78 |
like($appender->buffer, qr/ERROR - DBIx::Class::Exception => .* test dbix exception/, |
79 |
'Found test dbix exception in log'); |
80 |
$appender->{appender}->{buffer} = undef; |
81 |
}; |
82 |
|
83 |
subtest 'Koha::Exceptions::Exception' => sub { |
84 |
plan tests => 4; |
85 |
|
86 |
$t->get_ok('/default_exception_handling/koha') |
87 |
->status_is(500) |
88 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
89 |
like($appender->buffer, qr/ERROR - Koha::Exceptions::Exception => test koha exception/, |
90 |
'Found test koha exception in log'); |
91 |
$appender->{appender}->{buffer} = undef; |
92 |
}; |
93 |
|
94 |
subtest 'Unknown exception' => sub { |
95 |
plan tests => 4; |
96 |
|
97 |
$t->get_ok('/default_exception_handling/unknown') |
98 |
->status_is(500) |
99 |
->json_is('/error' => 'Something went wrong, check the logs.'); |
100 |
like($appender->buffer, qr/ERROR - Unknown::Exception::OhNo => {"what":"test unknown exception"}/, |
101 |
'Found test unknown exception in log'); |
102 |
$appender->{appender}->{buffer} = undef; |
103 |
}; |
104 |
}; |
105 |
|
106 |
sub add_default_exception_routes { |
107 |
my ($t) = @_; |
108 |
|
109 |
# Mojo::Exception |
110 |
$t->app->routes->get('/default_exception_handling/mojo' => sub { |
111 |
try { |
112 |
die "test mojo exception"; |
113 |
} catch { |
114 |
Koha::Exceptions::rethrow_exception($_); |
115 |
}; |
116 |
}); |
117 |
|
118 |
# die outside try-catch |
119 |
$t->app->routes->get('/default_exception_handling/dieoutsidetrycatch' => sub { |
120 |
die "die outside try-catch"; |
121 |
}); |
122 |
|
123 |
# DBIx::Class::Exception |
124 |
$t->app->routes->get('/default_exception_handling/dbix' => sub { |
125 |
package Koha::REST::V1::Test; |
126 |
try { |
127 |
DBIx::Class::Exception->throw('test dbix exception'); |
128 |
} catch { |
129 |
Koha::Exceptions::rethrow_exception($_); |
130 |
}; |
131 |
}); |
132 |
|
133 |
# Koha::Exceptions::Exception |
134 |
$t->app->routes->get('/default_exception_handling/koha' => sub { |
135 |
try { |
136 |
Koha::Exceptions::Exception->throw('test koha exception'); |
137 |
} catch { |
138 |
Koha::Exceptions::rethrow_exception($_); |
139 |
}; |
140 |
}); |
141 |
|
142 |
# Unknown exception |
143 |
$t->app->routes->get('/default_exception_handling/unknown' => sub { |
144 |
try { |
145 |
my $exception = { what => 'test unknown exception'}; |
146 |
bless $exception, 'Unknown::Exception::OhNo'; |
147 |
die $exception; |
148 |
} catch { |
149 |
Koha::Exceptions::rethrow_exception($_); |
150 |
}; |
151 |
}); |
152 |
} |
153 |
|
154 |
1; |