qbit/Exceptions.pm

filter: Statementstmt 41.2% 45 / 109 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 50.0% 13 / 26 Podpod 0.0% 0 / 6 totaltotal 24.0% 58 / 241 CC 35 SCAR 54.1
CC
35
Cov
24%
try
552.0
new
72.0
die_handler
12.0
CRAP 222.8
↑ Summary qbit/Date.pm »
1  
2 =head1 Name
3  
4 qbit::Exceptions - qbit exceptions
5  
6 =cut
7  
8 package qbit::Exceptions;
9 8 $qbit::Exceptions::VERSION = '2.7';
10 =head1 Synopsis
11  
12 Usage:
13  
14 package Exception::Sample;
15 use base qw(Exception);
16  
17 package Sample;
18 use qbit;
19  
20 sub ttt {
21 throw 'Fatal error';
22  
23 # or
24 # throw Exception::Sample;
25  
26 # or
27 # throw Exception::Sample 'Some text describing problem';
28 };
29  
30 1;
31  
32 One more sample. Here we are not catching proper exception, and the program
33 stops. Finally blocks are always executed.
34  
35 package Exception::Sample;
36 use base qw(Exception);
37  
38 package Exception::OtherSample;
39 use base qw(Exception);
40  
41 package Sample;
42 use qbit;
43  
44 sub ttt {
45 my ($self) = @_;
46  
47 try {
48 print "try\n";
49 throw Exception::Sample 'Exception message';
50 }
51 catch Exception::OtherSample with {
52 print "catch\n";
53 }
54 finally {
55 print "finally\n";
56 };
57  
58 print "end\n";
59 }
60  
61 1;
62  
63 And one more code example. Here we have exception hierarchy. We are throwing
64 a complex exception but we can catch it with it's parents.
65  
66 package Exception::Basic;
67 use base qw(Exception);
68  
69 package Exception::Complex;
70 use base qw(Exception::Basic);
71  
72 package Sample;
73 use qbit;
74  
75 sub ttt {
76 my ($self) = @_;
77  
78 try {
79 print "try\n";
80 throw Exception::Complex 'Exception message';
81 }
82 catch Exception::Basic with {
83 print "catch\n";
84 }
85 finally {
86 print "finally\n";
87 };
88  
89 print "end\n";
90 }
91  
92 1;
93  
94 In catch and finally blocks you can access $@ that stores exception object.
95  
96 =cut
97  
98 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 BEGIN {
104 8 ▶ our (@EXPORT, @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 8 @EXPORT = qw(try catch with finally throw);
107 8 @EXPORT_OK = @EXPORT;
108 }
109  
110 sub try(&;$) {
111 0 ▶ my ($sub, $catch) = @_;
Subroutine try: not called CC=23 SCAR=63.1
Pod undocumented
112  
113 0 eval {$sub->()};
114  
115 0 my $cur_catch = $catch;
116 0 ▶ my $find_catch = !defined($catch) || $catch->[0] eq '::FINALLY::';
Condition0%0/3
!defined($catch) || $catch->[0] eq '::FINALLY::'
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($catch) || $catch->[0] eq '::FINALLY::'
!defined($catch) $catch->[0] eq '::FINALLY::'
Truth table0%0/3
not(A) || B
A= !defined($catch)B= $catch->[0] eq '::FINALLY::'
ABresult
1X1
011
000
117  
118 0 my $first_exception = '';
119 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
120 0 ▶ $@ = Exception::SysDie->new($@)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $@ and $@->isa("Exception") 0 0 0
Condition0%0/3
ref $@ and $@->isa("Exception")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $@ and $@->isa("Exception")
ref $@ $@->isa("Exception")
Truth table0%0/3
A and B
A= ref $@B= $@->isa("Exception")
ABresult
0X0
100
111
121 unless ref($@) && $@->isa('Exception');
122  
123 0 $first_exception = $@;
124  
125 0 ▶ while (defined($cur_catch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cur_catch) 0 0 0
126 0 ▶ last if $cur_catch->[0] eq '::FINALLY::';
Branch: 1 branch
BranchTrueFalseTotal
if $cur_catch->[0] eq "::FINALLY::" 0 0 0
127 0 ▶ if ($find_catch || $@->isa($cur_catch->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($find_catch or $@->isa($cur_catch->[0])) { } 0 0 0
Condition0%0/3
$find_catch or $@->isa($cur_catch->[0])
l!l&&r!l&&!r
000
MC/DC0%0/2
$find_catch or $@->isa($cur_catch->[0])
$find_catch $@->isa($cur_catch->[0])
Truth table0%0/3
A or B
A= $find_catchB= $@->isa($cur_catch->[0])
ABresult
1X1
011
000
128 0 $find_catch = 1;
129 0 ▶ if (ref($cur_catch->[1]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cur_catch->[1] eq 'CODE') { } 0 0 0
130 0 eval {$cur_catch->[1]($first_exception)};
131  
132 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
133 0 $find_catch = 0;
134  
135 0 ▶ $@ = Exception::SysDie->new($@)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $@ and $@->isa("Exception") 0 0 0
Condition0%0/3
ref $@ and $@->isa("Exception")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $@ and $@->isa("Exception")
ref $@ $@->isa("Exception")
Truth table0%0/3
A and B
A= ref $@B= $@->isa("Exception")
ABresult
0X0
100
111
136 unless ref($@) && $@->isa('Exception');
137 }
138  
139 0 last;
140 } else {
141 0 $cur_catch = $cur_catch->[1];
142 }
143 } else {
144 0 ▶ $cur_catch = $cur_catch->[ref($cur_catch->[1]) eq 'CODE' ? 2 : 1];
Branch: 1 branch
BranchTrueFalseTotal
ref $cur_catch->[1] eq 'CODE' ? : 0 0 0
145 }
146 }
147 }
148  
149 0 ▶ $cur_catch = $cur_catch->[ref($cur_catch->[1]) eq 'CODE' ? 2 : 1]
Branch: 2 branches
BranchTrueFalseTotal
ref $cur_catch and defined $cur_catch and $cur_catch->[0] ne "::FINALLY::" and do { $cur_catch = $$cur_catch[ref $cur_catch->[1] eq "CODE" ? 2 : 1] } 0 0 0
ref $cur_catch->[1] eq 'CODE' ? : 0 0 0
Condition0%0/6
ref $cur_catch and defined $cur_catch and $cur_catch->[0] ne "::FINALLY::"
!ll&&!rl&&r
000
ref $cur_catch and defined $cur_catch
!ll&&!rl&&r
000
MC/DC0%0/3
ref $cur_catch and defined $cur_catch and $cur_catch->[0] ne "::FINALLY::"
ref $cur_catch defined $cur_catch $cur_catch->[0] ne "::FINALLY::"
Truth table0%0/4
A and B and C
A= ref $cur_catchB= defined $cur_catchC= $cur_catch->[0] ne "::FINALLY::"
ABCresult
0XX0
10X0
1100
1111
150 while ref($cur_catch) && defined($cur_catch) && $cur_catch->[0] ne '::FINALLY::';
151  
152 0 ▶ die("Expected semicolon after catch block (" . join(", ", (caller())[1, 2]) . ")\n")
Branch: 1 branch
BranchTrueFalseTotal
if defined $cur_catch and ref $cur_catch ne "ARRAY" 0 0 0
Condition0%0/3
defined $cur_catch and ref $cur_catch ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cur_catch and ref $cur_catch ne "ARRAY"
defined $cur_catch ref $cur_catch ne "ARRAY"
Truth table0%0/3
A and B
A= defined $cur_catchB= ref $cur_catch ne "ARRAY"
ABresult
0X0
100
111
153 if defined($cur_catch) && ref($cur_catch) ne 'ARRAY';
154  
155 0 ▶ $cur_catch->[1]($first_exception) if defined($cur_catch);
Branch: 1 branch
BranchTrueFalseTotal
if defined $cur_catch 0 0 0
156  
157 0 ▶ die $@ if $@ && !$find_catch;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $find_catch 0 0 0
Condition0%0/3
$@ and not $find_catch
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and not $find_catch
$@ not $find_catch
Truth table0%0/3
A and not(B)
A= $@B= not $find_catch
ABresult
0X0
100
111
158 }
159  
160 sub catch(&;$) {
161 0 ▶ return [Exception => @_];
Subroutine catch: not called CC=1 SCAR=6.9
Pod undocumented
162 }
163  
164 sub with(&;$) {
165 0 ▶ return @_;
Subroutine with: not called CC=1 SCAR=6.9
Pod undocumented
166 }
167  
168 sub finally(&;$) {
169 0 ▶ if (defined($_[1])) {die("Expected semicolon after finally block (" . join(", ", (caller())[1, 2]) . ")\n");}
Subroutine finally: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
170 0 return ['::FINALLY::' => @_];
171 }
172  
173 sub throw($) {
174 0 ▶ my ($exception) = @_;
Subroutine throw: not called CC=2 SCAR=17.9
Pod undocumented
175 0 ▶ $exception = Exception->new($exception) unless ref($exception);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $exception 0 0 0
176 0 die $exception;
177 }
178  
179 sub die_handler {
180 0 ▶ die @_ unless defined($^S); # Perl parser errors
Subroutine die_handler: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless defined $^S 0 0 0
181  
182 0 my ($exception) = @_;
183  
184 0 ▶ $exception = Exception::SysDie->new($exception) unless ref($exception);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $exception 0 0 0
185  
186 0 die $exception;
187 }
188  
189 package Exception;
190 8 $Exception::VERSION = '2.7';
191 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
193 8 ▶use overload '""' => sub {shift->as_string()};
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
194  
195 8 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
196  
197 sub new {
198 0 ▶ my ($this, $text, %data) = @_;
Subroutine new: not called CC=8 SCAR=42.8
199 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
200  
201 0 ▶ $text = '' if !defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
202  
203 0 my @call_stack = ();
204 0 my $i = 0;
205  
206 0 while (1) {
207  
208 package DB;
209 0 $DB::VERSION = '2.7';
210 0 my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, $is_require, $hints, $bitmask) =
211 caller($i);
212  
213 0 ▶ last if !defined($package);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package 0 0 0
214  
215 0 ▶ push(
Branch: 1 branch
BranchTrueFalseTotal
if $package ne "qbit::Exceptions" and $subroutine ne "qbit::Exceptions::try" 0 0 0
Condition0%0/3
$package ne "qbit::Exceptions" and $subroutine ne "qbit::Exceptions::try"
!ll&&!rl&&r
000
MC/DC0%0/2
$package ne "qbit::Exceptions" and $subroutine ne "qbit::Exceptions::try"
$package ne "qbit::Exceptions" $subroutine ne "qbit::Exceptions::try"
Truth table0%0/3
A and B
A= $package ne "qbit::Exceptions"B= $subroutine ne "qbit::Exceptions::try"
ABresult
0X0
100
111
216 @call_stack,
217 {
218 package => $package,
219 filename => $filename,
220 line => $line,
221 subroutine => $subroutine,
222 args => [@DB::args],
223 }
224 )
225 if $package ne 'qbit::Exceptions'
226 && $subroutine ne 'qbit::Exceptions::try';
227  
228 0 ++$i;
229 }
230  
231 0 my $caller = shift(@call_stack);
232 my $self = {
233 %data,
234 (
235 blessed($text) && $text->isa('Exception')
236 ? (text => $text->{'text'}, parent => $text)
237 : (text => $text)
238 ),
239 filename => $caller->{'filename'},
240 package => $caller->{'package'},
241 0 ▶ line => $caller->{'line'},
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($text) && $text->isa('Exception') ? : 0 0 0
Condition0%0/3
builtin::blessed($text) and $text->isa("Exception")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($text) and $text->isa("Exception")
builtin::blessed($text) $text->isa("Exception")
Truth table0%0/3
A and B
A= builtin::blessed($text)B= $text->isa("Exception")
ABresult
0X0
100
111
242 callstack => \@call_stack,
243 };
244  
245 0 bless $self, $class;
246 0 return $self;
247 }
248  
249 sub catch {
250 0 ▶ return \@_;
Subroutine catch: not called CC=1 SCAR=6.9
251 }
252  
253 sub throw {
254 0 ▶ qbit::Exceptions::throw(shift->new(@_));
Subroutine throw: not called CC=1 SCAR=6.9
255 }
256  
257 sub message {
258 0 ▶ return shift->{'text'};
Subroutine message: not called CC=1 SCAR=6.9
259 }
260  
261 sub as_string {
262 0 ▶ my ($self) = @_;
Subroutine as_string: not called CC=2 SCAR=17.9
263  
264 return
265 ref($self)
266 . ": $self->{'text'}\n"
267 . " Package: $self->{'package'}\n"
268 . " Filename: $self->{'filename'} (line $self->{'line'})\n"
269 . " CallStack:\n"
270 . ' '
271 . join("\n ",
272 0 ▶ map {$_->{'subroutine'} . "() called at '$_->{'filename'}' line $_->{'line'}"} @{$self->{'callstack'}})
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parent'} ? : 0 0 0
273 . "\n"
274 0 . ($self->{'parent'} ? "\n$self->{'parent'}\n" : '');
275 }
276  
277 package Exception::SysDie;
278 8 $Exception::SysDie::VERSION = '2.7';
279 8 ▶use base qw(Exception);
Subroutine BEGIN: called CC=1 SCAR=0.0
280  
281 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
282 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
283  
284 sub new {
285 0 ▶ my ($self, $text) = @_;
Subroutine new: not called CC=1 SCAR=6.9
286  
287 0 chomp($text);
288  
289 0 return $self->SUPER::new($text);
290 }
291  
292 package Exception::BadArguments;
293 8 $Exception::BadArguments::VERSION = '2.7';
294 8 ▶use base qw(Exception);
Subroutine BEGIN: called CC=1 SCAR=0.0
295  
296 package Exception::Denied;
297 8 $Exception::Denied::VERSION = '2.7';
298 8 ▶use base qw(Exception);
Subroutine BEGIN: called CC=1 SCAR=0.0
299  
300 8 1;
↑ Summary qbit/Date.pm »