Util/Exception.pm

filter: Statementstmt 80.0% 60 / 75 Branchbran 44.7% 17 / 38 Conditioncond 23.2% 10 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 50.0% 95 / 190 CC 37 SCAR 47.7
CC
37
Cov
56%
_throw
174.0
_trace
12.3
BEGIN
1.0
CRAP 117.4
« Util.pm ↑ Summary Util/Interface/Classic.pm »
1 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package File::Util::Exception;
5 5 $File::Util::Exception::VERSION = '4.201720';
6 # ABSTRACT: Base exception class for File::Util
7  
8 5 ▶use File::Util::Definitions qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 @ISA $AUTHORITY
12 @EXPORT_OK %EXPORT_TAGS
13 5 );
14  
15 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 $AUTHORITY = 'cpan:TOMMY';
18 5 @ISA = qw( Exporter );
19 5 @EXPORT_OK = qw( _throw );
20 5 %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
21  
22  
23 # --------------------------------------------------------
24 # File::Util::Exception::_throw
25 # --------------------------------------------------------
26 1 ▶sub _throw {
Subroutine _throw: called CC=29 SCAR=51.6
27  
28 1 my @in = @_;
29 1 my ( $this, $error_class, $error ) = splice @_, 0 , 3;
30 1 my $opts = $this->_remove_opts( \@_ );
31 1 my %fatal_rules = ();
32  
33 # here we handle support for the legacy error handling policy syntax,
34 # such as things like "fatals_as_status => 1"
35 #
36 # ...and we also handle support for the newer, more pretty error
37 # handling policy syntax using "onfail" keywords/subrefs
38  
39 $opts->{onfail} ||=
40 $opts->{opts} && ref $opts->{opts} eq 'HASH'
41 ? $opts->{opts}->{onfail}
42 1 ▶ : '';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'opts'} && ref $opts->{'opts'} eq 'HASH' ? : 1 0 1
Condition33%2/6
$opts->{"onfail"} ||= $opts->{"opts"} && ref $opts->{"opts"} eq "HASH" ? $opts->{"opts"}{"onfail"} : ""
l!l&&r!l&&!r
010
$opts->{"opts"} and ref $opts->{"opts"} eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/3
$opts->{"onfail"} ||= $opts->{"opts"} && ref $opts->{"opts"} eq "HASH" ? $opts->{"opts"}{"onfail"} : ""
$opts->{"onfail"} $opts->{"opts"} ref $opts->{"opts"} eq "HASH"
Truth table0%0/4
A ||= B and C
A= $opts->{"onfail"}B= $opts->{"opts"}C= ref $opts->{"opts"} eq "HASH"
ABCresult
1XX1
0111
00X0
0100
43  
44 1 ▶ $opts->{onfail} ||= $this->{opts}->{onfail};
Condition33%1/3
$opts->{"onfail"} ||= $this->{"opts"}{"onfail"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$opts->{"onfail"} ||= $this->{"opts"}{"onfail"}
$opts->{"onfail"} $this->{"opts"}{"onfail"}
Truth table33%1/3
A ||= B
A= $opts->{"onfail"}B= $this->{"opts"}{"onfail"}
ABresult
1X1
011
000
45  
46 1 ▶ $opts->{onfail} ||= 'die';
Condition50%1/2
$opts->{"onfail"} ||= "die"
l!l
10
MC/DC0%0/1
$opts->{"onfail"} ||= "die"
$opts->{"onfail"}
Truth table50%1/2
A
A= $opts->{"onfail"}
Aresult
11
00
47  
48 # fatalality-handling rules passed to the failing caller trump the
49 # rules set up in the attributes of the object; the mechanism below
50 # also allows for the implicit handling of fatals_are_fatal => 1
51 0 map { $fatal_rules{ $_ } = $_ }
52 grep /^fatals/o, keys %$opts;
53  
54 0 map { $fatal_rules{ $_ } = $_ }
55 1 grep /^fatals/o, keys %{ $opts->{opts} }
56 1 ▶ if $opts->{opts} && ref $opts->{opts} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"opts"} and ref $opts->{"opts"} eq "HASH" 1 0 1
Condition33%1/3
$opts->{"opts"} and ref $opts->{"opts"} eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
$opts->{"opts"} and ref $opts->{"opts"} eq "HASH"
$opts->{"opts"} ref $opts->{"opts"} eq "HASH"
Truth table33%1/3
A and B
A= $opts->{"opts"}B= ref $opts->{"opts"} eq "HASH"
ABresult
0X0
100
111
57  
58 1 ▶ unless ( scalar keys %fatal_rules ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %fatal_rules) 1 0 1
59 0 map { $fatal_rules{ $_ } = $_ }
60 1 grep /^fatals/o, keys %{ $this->{opts} }
61 }
62  
63 1 ▶ return 0 if $fatal_rules{fatals_as_status} || $opts->{onfail} eq 'zero';
Branch: 1 branch
BranchTrueFalseTotal
if $fatal_rules{"fatals_as_status"} or $opts->{"onfail"} eq "zero" 0 1 1
Condition33%1/3
$fatal_rules{"fatals_as_status"} or $opts->{"onfail"} eq "zero"
l!l&&r!l&&!r
001
MC/DC0%0/2
$fatal_rules{"fatals_as_status"} or $opts->{"onfail"} eq "zero"
$fatal_rules{"fatals_as_status"} $opts->{"onfail"} eq "zero"
Truth table33%1/3
A or B
A= $fatal_rules{"fatals_as_status"}B= $opts->{"onfail"} eq "zero"
ABresult
1X1
011
000
64  
65 1 ▶ return if $opts->{onfail} eq 'undefined';
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"onfail"} eq "undefined" 0 1 1
66  
67 1 my $is_plain;
68  
69 1 ▶ if ( !scalar keys %$opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not scalar keys %$opts) { } 0 1 1
70  
71 0 $opts->{_pak} = 'File::Util';
72  
73 0 $opts->{error} = $error;
74  
75 0 ▶ $error = $error ? 'plain error' : 'empty error';
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 0 0 0
76  
77 0 $is_plain++;
78 }
79 else {
80  
81 1 $opts->{_pak} = 'File::Util';
82  
83 1 ▶ $error ||= 'empty error';
Condition50%1/2
$error ||= "empty error"
l!l
10
MC/DC0%0/1
$error ||= "empty error"
$error
Truth table50%1/2
A
A= $error
Aresult
11
00
84  
85 1 ▶ if ( $error eq 'plain error' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error eq "plain error") 0 1 1
86  
87 0 ▶ $opts->{error} ||= shift @_;
Condition0%0/3
$opts->{"error"} ||= shift @_
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"error"} ||= shift @_
$opts->{"error"} shift @_
Truth table0%0/3
A ||= B
A= $opts->{"error"}B= shift @_
ABresult
1X1
011
000
88  
89 0 $is_plain++;
90 }
91 }
92  
93 1 my $bad_news = CORE::eval # tokenizing via stringy eval (is NOT evil)
94 (
95 '<<__ERRBLOCK__' . NL .
96 $error_class->_errors( $error ) . NL .
97 '__ERRBLOCK__'
98 );
99  
100 1 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'onfail'} eq 'warn' or $fatal_rules{'fatals_as_warning'}) { } 0 1 1
elsif ($opts->{'onfail'} eq 'message' or $fatal_rules{'fatals_as_errmsg'} or $opts->{'return'}) { } 0 1 1
Condition33%1/3
$opts->{"onfail"} eq "warn" or $fatal_rules{"fatals_as_warning"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$opts->{"onfail"} eq "warn" or $fatal_rules{"fatals_as_warning"}
$opts->{"onfail"} eq "warn" $fatal_rules{"fatals_as_warning"}
Truth table33%1/3
A or B
A= $opts->{"onfail"} eq "warn"B= $fatal_rules{"fatals_as_warning"}
ABresult
1X1
011
000
101 $opts->{onfail} eq 'warn' ||
102 $fatal_rules{fatals_as_warning}
103 ) {
104 0 ▶ warn _trace( $@ || $bad_news ) and return;
Branch: 1 branch
BranchTrueFalseTotal
warn _trace($@ || $bad_news) and return 0 0 0
Condition0%0/3
$@ || $bad_news
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $bad_news
$@ $bad_news
Truth table0%0/3
A || B
A= $@B= $bad_news
ABresult
1X1
011
000
105 }
106 ▶ elsif (
Condition33%2/6
$opts->{"onfail"} eq "message" or $fatal_rules{"fatals_as_errmsg"} or $opts->{"return"}
l!l&&r!l&&!r
001
$opts->{"onfail"} eq "message" or $fatal_rules{"fatals_as_errmsg"}
l!l&&r!l&&!r
001
MC/DC0%0/3
$opts->{"onfail"} eq "message" or $fatal_rules{"fatals_as_errmsg"} or $opts->{"return"}
$opts->{"onfail"} eq "message" $fatal_rules{"fatals_as_errmsg"} $opts->{"return"}
Truth table25%1/4
A or B or C
A= $opts->{"onfail"} eq "message"B= $fatal_rules{"fatals_as_errmsg"}C= $opts->{"return"}
ABCresult
1XX1
01X1
0011
0000
107 $opts->{onfail} eq 'message' ||
108 $fatal_rules{fatals_as_errmsg} ||
109 $opts->{return}
110 ) {
111 0 ▶ return _trace( $@ || $bad_news );
Condition0%0/3
$@ || $bad_news
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $bad_news
$@ $bad_news
Truth table0%0/3
A || B
A= $@B= $bad_news
ABresult
1X1
011
000
112 }
113  
114 1 ▶ warn _trace( $@ || $bad_news ) if $opts->{warn_also};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"warn_also"} 0 1 1
Condition0%0/3
$@ || $bad_news
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $bad_news
$@ $bad_news
Truth table0%0/3
A || B
A= $@B= $bad_news
ABresult
1X1
011
000
115  
116 die _trace( $@ || $bad_news )
117 1 ▶ unless ref $opts->{onfail} eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts->{"onfail"} eq "CODE" 0 1 1
Condition0%0/3
$@ || $bad_news
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $bad_news
$@ $bad_news
Truth table0%0/3
A || B
A= $@B= $bad_news
ABresult
1X1
011
000
118  
119 1 @_ = ( $bad_news, _trace() );
120  
121 1 goto $opts->{onfail};
122 }
123  
124  
125  
126 # --------------------------------------------------------
127 # File::Util::Exception::_trace
128 # --------------------------------------------------------
129 1 ▶sub _trace { # <<<<< this is not a class or object method!
Subroutine _trace: called CC=9 SCAR=25.1
130 1 my @errors = @_;
131  
132 my
133 (
134 1 $pak, $file, $line, $sub,
135 $hasargs, $wantarray, $evaltext, $req_OR_use,
136 @stack, $i, $frame_no
137 );
138  
139 1 $frame_no = 0;
140  
141 1 ▶ while
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i++) 6 1 7
142 (
143 ( $pak, $file, $line, $sub,
144 $hasargs, $wantarray, $evaltext, $req_OR_use
145 ) = caller( $i++ )
146 )
147 {
148 6 $frame_no = $i - 2;
149  
150 6 ▶ next unless $frame_no > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $frame_no > 0 2 4 6
151  
152 4 push @stack, <<__ERR__
153 $frame_no. $sub
154 -called at line ($line) of $file
155 4 ▶ @{[ $hasargs
Branch: 1 branch
BranchTrueFalseTotal
$hasargs ? : 4 0 4
156 ? '-was called with args'
157 : '-was called without args' ]}
158 4 ▶ @{[ $evaltext
Branch: 1 branch
BranchTrueFalseTotal
$evaltext ? : 0 4 4
159 ? '-was called to evalate text'
160 : '-was not called to evaluate anything' ]}
161 __ERR__
162 }
163  
164 1 $i = 0;
165  
166 1 for my $error ( @errors ) {
167  
168 0 ▶ $error = '' unless defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 0 0 0
169  
170 0 ▶ if ( !length $error ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $error) 0 0 0
171  
172 0 $error = qq{Something is wrong. Frame no. $frame_no...}
173 }
174  
175 0 ++$i;
176 }
177  
178 1 chomp for @errors;
179  
180 1 return join NL, @errors, @stack;
181 }
182  
183  
184 # --------------------------------------------------------
185 # File::Util::Exception::DESTROY()
186 # --------------------------------------------------------
187 1 ▶sub DESTROY { }
Subroutine DESTROY: called CC=1 SCAR=0.0
188  
189  
190 5 1;
191  
192  
193 __END__
« Util.pm ↑ Summary Util/Interface/Classic.pm »