lib/Badger/Exception.pm

filter: Statementstmt 92.7% 64 / 69 Branchbran 62.9% 34 / 54 Conditioncond 69.5% 16 / 23 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 84.6% 11 / 13 Podpod 100% 9 / 9 totaltotal 74.8% 137 / 183 CC 31 SCAR 18.1
CC
31
Cov
78%
trace
20.0
text
7.5
match_type
7.0
CRAP 6.1
« lib/Badger/Filesystem.pm ↑ Summary lib/Badger/Config.pm »
1 #========================================================================
2 #
3 # Badger::Exception
4 #
5 # DESCRIPTION
6 # Module implementing an exception class for reporting structured
7 # errors.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 #========================================================================
13  
14 package Badger::Exception;
15  
16 use Badger::Class
17 70 ▶ base => 'Badger::Base',
Subroutine BEGIN: called CC=1 SCAR=0.1
18 version => 0.01,
19 debug => 0,
20 mutators => 'type',
21 accessors => 'stack',
22 constants => 'TRUE ARRAY HASH DELIMITER',
23 import => 'class',
24 as_text => 'text', # overloaded text stringification
25 is_true => 1, # always evaluates to a true value
26 exports => {
27 hooks => {
28 trace => [
29 # args are ($self, $target, $symbol, $value)
30 1 ▶ sub { $TRACE = $_[3] },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 # expects one value argument
32 1
33 ],
34 colour => [
35 # args are ($self, $target, $symbol, $value)
36 0 ▶ sub { $COLOUR = $_[3] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 # expects one value argument
38 70 1
39 ],
40 },
41 },
42 messages => {
43 caller => "<4> called from <1>\n in <2> at line <3>",
44 70 };
45  
46 70 ▶use Badger::Rainbow ANSI => 'cyan yellow green';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 70 ▶our $FORMAT = '<type> error - <info>' unless defined $FORMAT;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Exception::FORMAT 70 0 70
48 70 ▶our $TYPE = 'undef' unless defined $TYPE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Exception::TYPE 70 0 70
49 70 ▶our $INFO = 'no information' unless defined $INFO;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Exception::INFO 70 0 70
50 70 ▶our $ANON = 'unknown' unless defined $ANON;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Exception::ANON 70 0 70
51 70 ▶our $TRACE = 0 unless defined $TRACE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Exception::TRACE 70 0 70
52 70 ▶our $COLOUR = 0 unless defined $COLOUR;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Badger::Exception::COLOUR 70 0 70
53  
54  
55 67 ▶sub init {
Subroutine init: called CC=4 SCAR=13.9
Pod documented
56 67 my ($self, $config) = @_;
57 67 ▶ $self->{ type } = $config->{ type } || $self->class->any_var('TYPE');
Condition66%2/3
$config->{'type'} || $self->class->any_var('TYPE')
l!l&&r!l&&!r
6430
MC/DC0%0/2
$config->{'type'} || $self->class->any_var('TYPE')
$config->{'type'} $self->class->any_var('TYPE')
Truth table66%2/3
A || B
A= $config->{'type'}B= $self->class->any_var('TYPE')
ABresult
1X1
011
000
58 67 ▶ $self->{ info } = $config->{ info } || '';
Condition100%2/2
$config->{'info'} || ''
l!l
643
MC/DC100%1/1
$config->{'info'} || ''
$config->{'info'}
Truth table100%2/2
A
A= $config->{'info'}
Aresult
11
00
59 67 $self->{ file } = $config->{ file };
60 67 $self->{ line } = $config->{ line };
61 # watch out for the case where 'trace' is set explicitly to 0
62 $self->{ trace } =
63 exists $config->{ trace }
64 ? $config->{ trace }
65 67 ▶ : $TRACE;
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'trace'} ? : 1 66 67
66  
67 67 return $self;
68 }
69  
70  
71 28 ▶sub info {
Subroutine info: called CC=3 SCAR=11.1
Pod documented
72 28 my $self = shift;
73 return @_
74 ? ($self->{ info } = shift)
75 28 ▶ : ($self->{ info } || $INFO);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 27 28
Condition66%2/3
$self->{"info"} or $Badger::Exception::INFO
l!l&&r!l&&!r
2610
MC/DC0%0/2
$self->{"info"} or $Badger::Exception::INFO
$self->{"info"} $Badger::Exception::INFO
Truth table66%2/3
A or B
A= $self->{"info"}B= $Badger::Exception::INFO
ABresult
1X1
011
000
76 }
77  
78  
79 3 ▶sub file {
Subroutine file: called CC=3 SCAR=11.7
Pod documented
80 3 my $self = shift;
81 return @_
82 ? ($self->{ file } = shift)
83 3 ▶ : ($self->{ file } || $ANON);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 3 3
Condition66%2/3
$self->{"file"} or $Badger::Exception::ANON
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"file"} or $Badger::Exception::ANON
$self->{"file"} $Badger::Exception::ANON
Truth table66%2/3
A or B
A= $self->{"file"}B= $Badger::Exception::ANON
ABresult
1X1
011
000
84 }
85  
86  
87 3 ▶sub line {
Subroutine line: called CC=3 SCAR=11.7
Pod documented
88 3 my $self = shift;
89 return @_
90 ? ($self->{ line } = shift)
91 3 ▶ : ($self->{ line } || $ANON);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 3 3
Condition66%2/3
$self->{"line"} or $Badger::Exception::ANON
l!l&&r!l&&!r
120
MC/DC0%0/2
$self->{"line"} or $Badger::Exception::ANON
$self->{"line"} $Badger::Exception::ANON
Truth table66%2/3
A or B
A= $self->{"line"}B= $Badger::Exception::ANON
ABresult
1X1
011
000
92 }
93  
94  
95 32 ▶sub text {
Subroutine text: called CC=7 SCAR=20.2
Pod documented
96 32 my $self = shift;
97 32 ▶ my $text = shift || $self->class->any_var('FORMAT');
Condition66%2/3
shift() || $self->class->any_var('FORMAT')
l!l&&r!l&&!r
1310
MC/DC0%0/2
shift() || $self->class->any_var('FORMAT')
shift() $self->class->any_var('FORMAT')
Truth table66%2/3
A || B
A= shift()B= $self->class->any_var('FORMAT')
ABresult
1X1
011
000
98  
99 # TODO: extend Badger::Utils::xprintf to handle this
100 32 ▶ $text =~ s/<(\w+)>/defined $self->{ $1 } ? $self->{ $1 } : "(no $1)"/eg;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$1} ? : 64 0 64
101  
102 # TODO: not sure we should add file and line automatically - better to
103 # leave it up to the $FORMAT
104 32 ▶ $text .= " in $self->{ file }" if $self->{ file };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"} 0 32 32
105 32 ▶ $text .= " at line $self->{ line }" if $self->{ line };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"line"} 0 32 32
106  
107 32 ▶ if ($self->{ trace } && (my $trace = $self->stack_trace)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trace"} and my $trace = $self->stack_trace) 2 30 32
Condition66%2/3
$self->{"trace"} and my $trace = $self->stack_trace
!ll&&!rl&&r
3002
MC/DC50%1/2
$self->{"trace"} and my $trace = $self->stack_trace
$self->{"trace"} my $trace = $self->stack_trace
Truth table66%2/3
A and B
A= $self->{"trace"}B= my $trace = $self->stack_trace
ABresult
0X0
100
111
108 2 $text .= "\n" . $trace;
109 }
110  
111 32 return $text;
112 }
113  
114  
115 2 ▶sub stack_trace {
Subroutine stack_trace: called CC=4 SCAR=14.1
Pod documented
116 2 my $self = shift;
117 2 my @lines;
118  
119 2 ▶ if (my $stack = $self->{ stack }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stack = $self->{"stack"}) 2 0 2
120 2 foreach my $caller (@$stack) {
121 7 ▶ my @args = $COLOUR
Branch: 1 branch
BranchTrueFalseTotal
$Badger::Exception::COLOUR ? : 0 7 7
122 ? (
123 cyan($caller->[0]),
124 cyan($caller->[1]),
125 yellow($caller->[2]),
126 yellow($caller->[3]),
127 )
128 : @$caller;
129 7 push(@lines, $self->message( caller => @args ));
130 }
131 }
132  
133 2 return join("\n", @lines);
134 }
135  
136  
137 0 ▶sub trace {
Subroutine trace: not called CC=4 SCAR=30.0
Pod documented
138 0 my $self = shift;
139 0 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
140 return @_
141 ? ($self->{ trace } = shift )
142 0 ▶ : $self->{ trace };
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
143 }
144 else {
145 return @_
146 0 ▶ ? $self->class->var( TRACE => shift )
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
147 : $self->class->var('TRACE');
148 }
149 }
150  
151 61 ▶sub throw {
Subroutine throw: called CC=4 SCAR=13.9
Pod documented
152 61 my $self = shift;
153  
154 # save relevant information from caller stack for enhanced debugging,
155 # but only the first time the exception is thrown
156 61 ▶ if ($self->{ trace } && ! $self->{ stack }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trace"} and not $self->{"stack"}) 2 59 61
Condition66%2/3
$self->{"trace"} and not $self->{"stack"}
!ll&&!rl&&r
5902
MC/DC50%1/2
$self->{"trace"} and not $self->{"stack"}
$self->{"trace"} not $self->{"stack"}
Truth table66%2/3
A and not(B)
A= $self->{"trace"}B= not $self->{"stack"}
ABresult
0X0
100
111
157 2 my @stack;
158 2 my $i = 1;
159 2 while (1) {
160 9 my @info = caller($i++);
161 9 ▶ last unless @info;
Branch: 1 branch
BranchTrueFalseTotal
unless @info 2 7 9
162 7 push(@stack, \@info);
163 }
164 2 $self->{ stack } = \@stack;
165 }
166  
167 61 die $self;
168 }
169  
170  
171  
172  
173 #------------------------------------------------------------------------
174 # match_type(@types)
175 #
176 # Selects the most appropriate handler for the current exception type,
177 # from the list of types passed in as arguments. The method returns the
178 # item which is an exact match for type or the closest, more
179 # generic handler (e.g. foo being more generic than foo.bar, etc.)
180 #------------------------------------------------------------------------
181  
182 7 ▶sub match_type {
Subroutine match_type: called CC=7 SCAR=19.5
Pod documented
183 7 my $self = shift;
184 7 ▶ my $types = @_ == 1 ? shift : [@_];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 3 4 7
185 7 my $type = $self->{ type };
186  
187 7 ▶ $types = [ split(DELIMITER, $types) ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $types 1 6 7
188 unless ref $types;
189  
190 7 ▶ $types = { map { $_ => $_ } @$types }
Branch: 1 branch
BranchTrueFalseTotal
if ref $types eq "ARRAY" 6 1 7
191 if ref $types eq ARRAY;
192  
193 7 ▶ return $self->error( invalid => 'type match' => $types )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $types eq "HASH" 0 7 7
194 unless ref $types eq HASH;
195  
196 7 ▶ while ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 16 1 17
197 return $types->{ $type }
198 16 ▶ if $types->{ $type };
Branch: 1 branch
BranchTrueFalseTotal
if $types->{$type} 6 10 16
199  
200 # strip .element from the end of the exception type to find a
201 # more generic handler
202 10 $type =~ s/\.?[^\.]*$//;
203 }
204  
205 1 return undef;
206 }
207  
208  
209  
210 70 1;
211 __END__
« lib/Badger/Filesystem.pm ↑ Summary lib/Badger/Config.pm »