lib/File/Util/Exception.pm

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