blib/lib/Badger/Exception.pm untested

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