-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path17handle_error.t
205 lines (158 loc) · 4.16 KB
/
17handle_error.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!perl -w
use strict;
use warnings;
use DBI;
use Test::More;
my $skip_error;
my $skip_warn;
my $handled_errstr;
sub error_sub {
my ($errstr, $dbh, $ret) = @_;
$handled_errstr = $errstr;
$handled_errstr =~ s/.* set_err (?:failed|warning): //;
return $ret unless ($skip_error and $errstr =~ / set_err failed: /) or ($skip_warn and $errstr =~ / set_err warning:/);
$dbh->set_err(undef, undef);
return 1;
}
my $dbh = DBI->connect('dbi:ExampleP:.', undef, undef, { PrintError => 0, RaiseError => 0, PrintWarn => 0, RaiseWarn => 0, HandleError => \&error_sub });
sub clear_err {
$dbh->set_err(undef, undef);
$handled_errstr = undef;
}
###
ok eval { $dbh->set_err('', 'string 1'); 1 } or diag($@);
is $dbh->err, '';
is $dbh->errstr, 'string 1';
is $handled_errstr, undef;
clear_err;
ok eval { $dbh->set_err(0, 'string 2'); 1 } or diag($@);
is $dbh->err, 0;
is $dbh->errstr, 'string 2';
is $handled_errstr, undef;
clear_err;
ok eval { $dbh->set_err(1, 'string 3'); 1 } or diag($@);
is $dbh->err, 1;
is $dbh->errstr, 'string 3';
is $handled_errstr, 'string 3';
clear_err;
###
$dbh->{RaiseError} = 1;
ok eval { $dbh->set_err('', 'string 4'); 1 } or diag($@);
is $dbh->err, '';
is $dbh->errstr, 'string 4';
is $handled_errstr, undef;
clear_err;
ok eval { $dbh->set_err(0, 'string 5'); 1 } or diag($@);
is $dbh->err, 0;
is $dbh->errstr, 'string 5';
is $handled_errstr, undef;
clear_err;
ok !eval { $dbh->set_err(1, 'string 6'); 1 };
is $dbh->err, 1;
is $dbh->errstr, 'string 6';
is $handled_errstr, 'string 6';
clear_err;
$dbh->{RaiseError} = 0;
###
$dbh->{RaiseWarn} = 1;
ok eval { $dbh->set_err('', 'string 7'); 1 } or diag($@);
is $dbh->err, '';
is $dbh->errstr, 'string 7';
is $handled_errstr, undef;
clear_err;
ok !eval { $dbh->set_err(0, 'string 8'); 1 };
is $dbh->err, 0;
is $dbh->errstr, 'string 8';
is $handled_errstr, 'string 8';
clear_err;
ok eval { $dbh->set_err(1, 'string 9'); 1 } or diag($@);
is $dbh->err, 1;
is $dbh->errstr, 'string 9';
is $handled_errstr, 'string 9';
clear_err;
$dbh->{RaiseWarn} = 0;
###
$dbh->{RaiseError} = 1;
$dbh->{RaiseWarn} = 1;
ok eval { $dbh->set_err('', 'string 10'); 1 } or diag($@);
is $dbh->err, '';
is $dbh->errstr, 'string 10';
is $handled_errstr, undef;
clear_err;
ok !eval { $dbh->set_err(0, 'string 11'); 1 };
is $dbh->err, 0;
is $dbh->errstr, 'string 11';
is $handled_errstr, 'string 11';
clear_err;
ok !eval { $dbh->set_err(1, 'string 12'); 1 };
is $dbh->err, 1;
is $dbh->errstr, 'string 12';
is $handled_errstr, 'string 12';
clear_err;
$dbh->{RaiseError} = 0;
$dbh->{RaiseWarn} = 0;
###
$dbh->{RaiseError} = 1;
$skip_error = 1;
ok eval { $dbh->set_err('', 'string 13'); 1 } or diag($@);
is $dbh->err, '';
is $dbh->errstr, 'string 13';
is $handled_errstr, undef;
clear_err;
ok eval { $dbh->set_err(0, 'string 14'); 1 } or diag($@);
is $dbh->err, 0;
is $dbh->errstr, 'string 14';
is $handled_errstr, undef;
clear_err;
ok eval { $dbh->set_err(1, 'string 15'); 1 } or diag($@);
is $dbh->err, undef;
is $dbh->errstr, undef;
is $handled_errstr, 'string 15';
clear_err;
$dbh->{RaiseError} = 0;
$skip_error = 0;
###
$dbh->{RaiseWarn} = 1;
$skip_warn = 1;
ok eval { $dbh->set_err('', 'string 16'); 1 } or diag($@);
is $dbh->err, '';
is $dbh->errstr, 'string 16';
is $handled_errstr, undef;
clear_err;
ok eval { $dbh->set_err(0, 'string 17'); 1 } or diag($@);
is $dbh->err, undef;
is $dbh->errstr, undef;
is $handled_errstr, 'string 17';
clear_err;
ok eval { $dbh->set_err(1, 'string 18'); 1 } or diag($@);
is $dbh->err, 1;
is $dbh->errstr, 'string 18';
is $handled_errstr, 'string 18';
clear_err;
$dbh->{RaiseWarn} = 0;
$skip_error = 0;
###
$dbh->{RaiseError} = 1;
$dbh->{RaiseWarn} = 1;
$skip_error = 1;
$skip_warn = 1;
ok eval { $dbh->set_err('', 'string 19'); 1 } or diag($@);
is $dbh->err, '';
is $dbh->errstr, 'string 19';
is $handled_errstr, undef;
clear_err;
ok eval { $dbh->set_err(0, 'string 20'); 1 } or diag($@);
is $dbh->err, undef;
is $dbh->errstr, undef;
is $handled_errstr, 'string 20';
clear_err;
ok eval { $dbh->set_err(1, 'string 21'); 1 } or diag($@);
is $dbh->err, undef;
is $dbh->errstr, undef;
is $handled_errstr, 'string 21';
clear_err;
$dbh->{RaiseError} = 0;
$dbh->{RaiseWarn} = 0;
$skip_error = 0;
###
done_testing;