| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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 |
|
11
|
|
|
|
|
|
|
# |
|
12
|
|
|
|
|
|
|
#======================================================================== |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
package Badger::Exception; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use Badger::Class |
|
17
|
|
|
|
|
|
|
base => 'Badger::Base', |
|
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
|
|
|
|
|
2
|
sub { $TRACE = $_[3] }, |
|
31
|
|
|
|
|
|
|
# expects one value argument |
|
32
|
|
|
|
|
|
|
1 |
|
33
|
|
|
|
|
|
|
], |
|
34
|
|
|
|
|
|
|
colour => [ |
|
35
|
|
|
|
|
|
|
# args are ($self, $target, $symbol, $value) |
|
36
|
0
|
|
|
|
|
0
|
sub { $COLOUR = $_[3] }, |
|
37
|
|
|
|
|
|
|
# expects one value argument |
|
38
|
70
|
|
|
|
|
866
|
1 |
|
39
|
|
|
|
|
|
|
], |
|
40
|
|
|
|
|
|
|
}, |
|
41
|
|
|
|
|
|
|
}, |
|
42
|
|
|
|
|
|
|
messages => { |
|
43
|
|
|
|
|
|
|
caller => "<4> called from <1>\n in <2> at line <3>", |
|
44
|
70
|
|
|
70
|
|
424
|
}; |
|
|
70
|
|
|
|
|
105
|
|
|
45
|
|
|
|
|
|
|
|
|
46
|
70
|
|
|
70
|
|
482
|
use Badger::Rainbow ANSI => 'cyan yellow green'; |
|
|
70
|
|
|
|
|
115
|
|
|
|
70
|
|
|
|
|
349
|
|
|
47
|
|
|
|
|
|
|
our $FORMAT = ' error - ' unless defined $FORMAT; |
|
48
|
|
|
|
|
|
|
our $TYPE = 'undef' unless defined $TYPE; |
|
49
|
|
|
|
|
|
|
our $INFO = 'no information' unless defined $INFO; |
|
50
|
|
|
|
|
|
|
our $ANON = 'unknown' unless defined $ANON; |
|
51
|
|
|
|
|
|
|
our $TRACE = 0 unless defined $TRACE; |
|
52
|
|
|
|
|
|
|
our $COLOUR = 0 unless defined $COLOUR; |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub init { |
|
56
|
69
|
|
|
69
|
1
|
126
|
my ($self, $config) = @_; |
|
57
|
69
|
|
66
|
|
|
886
|
$self->{ type } = $config->{ type } || $self->class->any_var('TYPE'); |
|
58
|
69
|
|
100
|
|
|
163
|
$self->{ info } = $config->{ info } || ''; |
|
59
|
69
|
|
|
|
|
110
|
$self->{ file } = $config->{ file }; |
|
60
|
69
|
|
|
|
|
102
|
$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
|
69
|
100
|
|
|
|
184
|
: $TRACE; |
|
66
|
|
|
|
|
|
|
|
|
67
|
69
|
|
|
|
|
123
|
return $self; |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub info { |
|
72
|
28
|
|
|
28
|
1
|
97
|
my $self = shift; |
|
73
|
|
|
|
|
|
|
return @_ |
|
74
|
|
|
|
|
|
|
? ($self->{ info } = shift) |
|
75
|
28
|
100
|
66
|
|
|
157
|
: ($self->{ info } || $INFO); |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub file { |
|
80
|
3
|
|
|
3
|
1
|
4
|
my $self = shift; |
|
81
|
|
|
|
|
|
|
return @_ |
|
82
|
|
|
|
|
|
|
? ($self->{ file } = shift) |
|
83
|
3
|
50
|
66
|
|
|
14
|
: ($self->{ file } || $ANON); |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub line { |
|
88
|
3
|
|
|
3
|
1
|
4
|
my $self = shift; |
|
89
|
|
|
|
|
|
|
return @_ |
|
90
|
|
|
|
|
|
|
? ($self->{ line } = shift) |
|
91
|
3
|
50
|
66
|
|
|
15
|
: ($self->{ line } || $ANON); |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub text { |
|
96
|
34
|
|
|
34
|
1
|
59
|
my $self = shift; |
|
97
|
34
|
|
66
|
|
|
169
|
my $text = shift || $self->class->any_var('FORMAT'); |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# TODO: extend Badger::Utils::xprintf to handle this |
|
100
|
34
|
50
|
|
|
|
239
|
$text =~ s/<(\w+)>/defined $self->{ $1 } ? $self->{ $1 } : "(no $1)"/eg; |
|
|
68
|
|
|
|
|
289
|
|
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# TODO: not sure we should add file and line automatically - better to |
|
103
|
|
|
|
|
|
|
# leave it up to the $FORMAT |
|
104
|
34
|
50
|
|
|
|
93
|
$text .= " in $self->{ file }" if $self->{ file }; |
|
105
|
34
|
50
|
|
|
|
65
|
$text .= " at line $self->{ line }" if $self->{ line }; |
|
106
|
|
|
|
|
|
|
|
|
107
|
34
|
100
|
66
|
|
|
81
|
if ($self->{ trace } && (my $trace = $self->stack_trace)) { |
|
108
|
2
|
|
|
|
|
6
|
$text .= "\n" . $trace; |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
34
|
|
|
|
|
204
|
return $text; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
sub stack_trace { |
|
116
|
2
|
|
|
2
|
1
|
5
|
my $self = shift; |
|
117
|
2
|
|
|
|
|
2
|
my @lines; |
|
118
|
|
|
|
|
|
|
|
|
119
|
2
|
50
|
|
|
|
5
|
if (my $stack = $self->{ stack }) { |
|
120
|
2
|
|
|
|
|
4
|
foreach my $caller (@$stack) { |
|
121
|
7
|
50
|
|
|
|
29
|
my @args = $COLOUR |
|
122
|
|
|
|
|
|
|
? ( |
|
123
|
|
|
|
|
|
|
cyan($caller->[0]), |
|
124
|
|
|
|
|
|
|
cyan($caller->[1]), |
|
125
|
|
|
|
|
|
|
yellow($caller->[2]), |
|
126
|
|
|
|
|
|
|
yellow($caller->[3]), |
|
127
|
|
|
|
|
|
|
) |
|
128
|
|
|
|
|
|
|
: @$caller; |
|
129
|
7
|
|
|
|
|
26
|
push(@lines, $self->message( caller => @args )); |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
2
|
|
|
|
|
11
|
return join("\n", @lines); |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub trace { |
|
138
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
139
|
0
|
0
|
|
|
|
0
|
if (ref $self) { |
|
140
|
|
|
|
|
|
|
return @_ |
|
141
|
|
|
|
|
|
|
? ($self->{ trace } = shift ) |
|
142
|
0
|
0
|
|
|
|
0
|
: $self->{ trace }; |
|
143
|
|
|
|
|
|
|
} |
|
144
|
|
|
|
|
|
|
else { |
|
145
|
|
|
|
|
|
|
return @_ |
|
146
|
0
|
0
|
|
|
|
0
|
? $self->class->var( TRACE => shift ) |
|
147
|
|
|
|
|
|
|
: $self->class->var('TRACE'); |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
} |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub throw { |
|
152
|
63
|
|
|
63
|
1
|
110
|
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
|
63
|
100
|
66
|
|
|
187
|
if ($self->{ trace } && ! $self->{ stack }) { |
|
157
|
2
|
|
|
|
|
4
|
my @stack; |
|
158
|
2
|
|
|
|
|
2
|
my $i = 1; |
|
159
|
2
|
|
|
|
|
4
|
while (1) { |
|
160
|
9
|
|
|
|
|
38
|
my @info = caller($i++); |
|
161
|
9
|
100
|
|
|
|
19
|
last unless @info; |
|
162
|
7
|
|
|
|
|
10
|
push(@stack, \@info); |
|
163
|
|
|
|
|
|
|
} |
|
164
|
2
|
|
|
|
|
8
|
$self->{ stack } = \@stack; |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
|
|
167
|
63
|
|
|
|
|
385
|
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
|
|
|
|
|
|
|
sub match_type { |
|
183
|
7
|
|
|
7
|
1
|
10
|
my $self = shift; |
|
184
|
7
|
100
|
|
|
|
14
|
my $types = @_ == 1 ? shift : [@_]; |
|
185
|
7
|
|
|
|
|
12
|
my $type = $self->{ type }; |
|
186
|
|
|
|
|
|
|
|
|
187
|
7
|
100
|
|
|
|
21
|
$types = [ split(DELIMITER, $types) ] |
|
188
|
|
|
|
|
|
|
unless ref $types; |
|
189
|
|
|
|
|
|
|
|
|
190
|
7
|
100
|
|
|
|
15
|
$types = { map { $_ => $_ } @$types } |
|
|
24
|
|
|
|
|
38
|
|
|
191
|
|
|
|
|
|
|
if ref $types eq ARRAY; |
|
192
|
|
|
|
|
|
|
|
|
193
|
7
|
50
|
|
|
|
14
|
return $self->error( invalid => 'type match' => $types ) |
|
194
|
|
|
|
|
|
|
unless ref $types eq HASH; |
|
195
|
|
|
|
|
|
|
|
|
196
|
7
|
|
|
|
|
10
|
while ($type) { |
|
197
|
|
|
|
|
|
|
return $types->{ $type } |
|
198
|
16
|
100
|
|
|
|
40
|
if $types->{ $type }; |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
# strip .element from the end of the exception type to find a |
|
201
|
|
|
|
|
|
|
# more generic handler |
|
202
|
10
|
|
|
|
|
46
|
$type =~ s/\.?[^\.]*$//; |
|
203
|
|
|
|
|
|
|
} |
|
204
|
|
|
|
|
|
|
|
|
205
|
1
|
|
|
|
|
4
|
return undef; |
|
206
|
|
|
|
|
|
|
} |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
1; |
|
211
|
|
|
|
|
|
|
__END__ |