lib/Bio/NEXUS/Util/Exceptions.pm

filter: Statementstmt 94.3% 199 / 211 Branchbran 61.1% 33 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.8% 46 / 49 Podpod 100% 5 / 5 totaltotal 88.7% 283 / 319 CC 31 SCAR 10.4
CC
31
Cov
88%
caught
20.0
throw
4.1
_skip_me
3.1
CRAP 2.8
« lib/Bio/NEXUS/TaxaBlock.pm ↑ Summary
1 ######################################################
2 # Exceptions.pm - Exception classes for Bio::NEXUS.
3 ######################################################
4 # original version thanks to Rutger
5 #
6 # $Id: Exceptions.pm,v 1.5 2012/02/07 21:49:27 astoltzfus Exp $
7 #
8 package Bio::NEXUS::Util::StackTrace;
9 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
12 13 my $class = shift;
13 13 my $self = [];
14 13 my $i = 0;
15 13 my $j = 0;
16 package DB; # to get @_ stack from previous frames, see perldoc -f caller
17 13 ▶ while( my @frame = caller($i) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@frame) = caller $i) 108 13 121
18 108 my $package = $frame[0];
19 108 ▶ if ( not Bio::NEXUS::Util::StackTrace::_skip_me( $package ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Bio::NEXUS::Util::StackTrace::_skip_me($package)) 77 31 108
20 77 my @args = @DB::args;
21 77 $self->[$j++] = [ @frame, @args ];
22 }
23 108 $i++;
24 }
25 package Bio::NEXUS::Util::StackTrace;
26 13 shift @$self; # to remove "throw" frame
27 13 return bless $self, $class;
28 }
29  
30 108 ▶sub _skip_me {
Subroutine _skip_me: called CC=3 SCAR=11.2
31 108 my $class = shift;
32 108 my $skip = 0;
33 108 ▶ if ( UNIVERSAL::isa( $class, 'Bio::NEXUS::Util::Exceptions') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($class, "Bio::NEXUS::Util::Exceptions")) 31 77 108
34 31 $skip++;
35 }
36 108 ▶ if ( UNIVERSAL::isa( $class, 'Bio::NEXUS::Util::ExceptionFactory' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($class, "Bio::NEXUS::Util::ExceptionFactory")) 0 108 108
37 0 $skip++;
38 }
39 108 return $skip;
40 }
41  
42 # fields in frame:
43 # [
44 # 0 'main',
45 # +1 '/Users/rvosa/Desktop/exceptions.pl',
46 # +2 102,
47 # +3 'Object::this_dies',
48 # 4 1,
49 # 5 undef,
50 # 6 undef,
51 # 7 undef,
52 # 8 2,
53 # 9 'UUUUUUUUUUUU',
54 # +10 bless( {}, 'Object' ),
55 # +11 'very',
56 # +12 'violently'
57 # ],
58  
59 14 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=11.0
60 14 my $self = shift;
61 14 my $string = "";
62 14 for my $frame ( @$self ) {
63 66 my $method = $frame->[3];
64 66 my @args;
65 66 for my $i ( 10 .. $#{ $frame } ) {
66 260 push @args, $frame->[$i];
67 }
68 66 my $file = $frame->[1];
69 66 my $line = $frame->[2];
70 35 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 66 $string .= $method . "(" . join(', ', map { "'$_'" } @args ) . ") called at $file line $line\n";
72 }
73 14 return $string;
74 }
75  
76 package Bio::NEXUS::Util::Exceptions;
77 35 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
78 35 require Exporter;
79 35 ▶ use vars qw($AUTOLOAD @EXPORT_OK @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
80 35 @ISA=qw(Exporter);
81 35 @EXPORT_OK=qw(throw);
82 }
83 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 0 ▶use overload 'bool' => sub { 1 }, 'fallback' => 1, '""' => \&as_string;
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
85  
86 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
87 13 my $class = shift;
88 13 my %args = @_;
89 my $self = {
90 'error' => $args{'error'},
91 13 'description' => $args{'description'},
92 'trace' => Bio::NEXUS::Util::StackTrace->new,
93 'time' => CORE::time(),
94 'pid' => $$,
95 'uid' => $<,
96 'euid' => $>,
97 'gid' => $(,
98 'egid' => $),
99 };
100 13 return bless $self, $class;
101 }
102  
103 14 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
104 14 my $self = shift;
105 14 my $error = $self->error;
106 14 my $description = $self->description;
107 14 my $class = ref $self;
108 14 my $trace = join "\n", map { "STACK: $_" } split '\n', $self->trace->as_string;
109 14 return <<"ERROR_HERE_DOC";
110 -------------------------- EXCEPTION ----------------------------
111 Message: $error
112  
113 An exception of type $class
114 was thrown.
115  
116 $description
117  
118 Refer to the Bio::NEXUS::Util::Exceptions documentation for more
119 information.
120 ------------------------- STACK TRACE ---------------------------
121 $trace
122 -----------------------------------------------------------------
123 ERROR_HERE_DOC
124 }
125  
126 13 ▶sub throw ($$) {
Subroutine throw: called CC=4 SCAR=14.0
Pod documented
127 # called as static method
128 13 ▶ if ( scalar @_ == 3 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 3) { } 8 5 13
elsif (scalar @_ == 2) { } 5 0 5
129 8 my $class = shift;
130 8 my $self = $class->new(@_);
131 8 die $self;
132 }
133 # called as function, e.g. throw BadArgs => 'msg';
134 elsif ( scalar @_ == 2 ) {
135 5 my $type = shift;
136 5 my $class = __PACKAGE__ . '::' . $type;
137 5 my $self;
138 5 eval {
139 5 $self = $class->new( 'error' => shift );
140 };
141 5 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 5 5
142 0 die Bio::NEXUS::Util::Exceptions::API->new( 'error' => "Can't throw errors of type $type: $@" );
143 }
144 else {
145 5 die $self;
146 }
147 }
148 }
149  
150 0 ▶sub rethrow {
Subroutine rethrow: not called CC=1 SCAR=6.9
Pod documented
151 0 my $self = shift;
152 0 die $self;
153 }
154  
155 0 ▶sub caught {
Subroutine caught: not called CC=4 SCAR=30.0
Pod documented
156 0 my $class = shift;
157 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
158 0 $class = shift;
159 }
160 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
161 0 ▶ if ( UNIVERSAL::isa( $@, $class ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($@, $class)) { } 0 0 0
162 0 return $@;
163 }
164 else {
165 0 die $@;
166 }
167 }
168 }
169  
170 41 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
171 41 my $self = shift;
172 41 my $field = $AUTOLOAD;
173 41 $field =~ s/.*://;
174 41 return $self->{$field};
175 }
176  
177 35 ▶sub _make_exceptions {
Subroutine _make_exceptions: called CC=3 SCAR=11.0
178 35 my $class = shift;
179 35 my $root = shift;
180 35 my %exceptions = @_;
181 35 for my $exception ( keys %exceptions ) {
182 560 my $isa = $exceptions{ $exception }->{'isa'};
183 560 ▶ my @isa = ref $isa ? @$isa : ( $isa );
Branch: 1 branch
BranchTrueFalseTotal
ref $isa ? : 70 490 560
184 560 my $description = $exceptions{ $exception }->{'description'};
185 560 my $class = <<"EXCEPTION_CLASS";
186 package ${exception};
187 use vars '\@ISA';
188 \@ISA=qw(@isa);
189 my \$desc;
190 sub description {
191 my \$self = shift;
192 if ( \@_ ) {
193 \$desc = shift;
194 }
195 return \$desc;
196 }
197 1;
198 EXCEPTION_CLASS
199 35 ▶ eval $class;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Subroutine description: called CC=2 SCAR=6.9
Branch: 16 branches
BranchTrueFalseTotal
if (@_) 35 0 35
if (@_) 35 0 35
if (@_) 35 0 35
if (@_) 35 0 35
if (@_) 35 0 35
if (@_) 35 0 35
if (@_) 35 10 45
if (@_) 35 0 35
if (@_) 35 0 35
if (@_) 35 0 35
if (@_) 35 0 35
if (@_) 35 0 35
if (@_) 35 2 37
if (@_) 35 0 35
if (@_) 35 1 36
if (@_) 35 1 36
200 560 $exception->description( $description );
201 }
202
203 }
204  
205 35 __PACKAGE__->_make_exceptions(
206 # root classes
207 'Bio::NEXUS::Util::Exceptions',
208 'Bio::NEXUS::Util::Exceptions::Generic' => {
209 'isa' => 'Bio::NEXUS::Util::Exceptions',
210 'description' => "No further details about this type of error are available."
211 },
212
213 # exceptions on method calls
214 'Bio::NEXUS::Util::Exceptions::API' => {
215 'isa' => 'Bio::NEXUS::Util::Exceptions::Generic',
216 'description' => "No more details about this type of error are available."
217 },
218 'Bio::NEXUS::Util::Exceptions::UnknownMethod' => {
219 'isa' => 'Bio::NEXUS::Util::Exceptions::API',
220 'description' => "This kind of error happens when a non-existent method is called.",
221 },
222 'Bio::NEXUS::Util::Exceptions::NotImplemented' => {
223 'isa' => 'Bio::NEXUS::Util::Exceptions::API',
224 'description' => "This kind of error happens when a non-implemented\n(interface) method is called.",
225 },
226 'Bio::NEXUS::Util::Exceptions::Deprecated' => {
227 'isa' => 'Bio::NEXUS::Util::Exceptions::API',
228 'description' => "This kind of error happens when a deprecated method is called.",
229 },
230  
231 # exceptions on arguments
232 'Bio::NEXUS::Util::Exceptions::BadArgs' => {
233 'isa' => 'Bio::NEXUS::Util::Exceptions::Generic',
234 'description' => "This kind of error happens when bad or incomplete arguments\nare provided.",
235 },
236 'Bio::NEXUS::Util::Exceptions::BadString' => {
237 'isa' => 'Bio::NEXUS::Util::Exceptions::BadArgs',
238 'description' => "This kind of error happens when an unsafe string argument is\nprovided.",
239 },
240 'Bio::NEXUS::Util::Exceptions::OddHash' => {
241 'isa' => 'Bio::NEXUS::Util::Exceptions::BadArgs',
242 'description' => "This kind of error happens when an uneven number\nof arguments (so no key/value pairs) was provided.",
243 },
244 'Bio::NEXUS::Util::Exceptions::ObjectMismatch' => {
245 'isa' => 'Bio::NEXUS::Util::Exceptions::BadArgs',
246 'description' => "This kind of error happens when an invalid object\nargument is provided.",
247 },
248 'Bio::NEXUS::Util::Exceptions::InvalidData' => {
249 'isa' => [
250 'Bio::NEXUS::Util::Exceptions::BadString',
251 'Bio::NEXUS::Util::Exceptions::BadFormat',
252 ],
253 'description' => "This kind of error happens when invalid character data is\nprovided."
254 },
255 'Bio::NEXUS::Util::Exceptions::OutOfBounds' => {
256 'isa' => 'Bio::NEXUS::Util::Exceptions::BadArgs',
257 'description' => "This kind of error happens when an index is outside of its range.",
258 },
259 'Bio::NEXUS::Util::Exceptions::BadNumber' => {
260 'isa' => 'Bio::NEXUS::Util::Exceptions::Generic',
261 'description' => "This kind of error happens when an invalid numerical argument\nis provided.",
262 },
263  
264 # system exceptions
265 'Bio::NEXUS::Util::Exceptions::System' => {
266 'isa' => 'Bio::NEXUS::Util::Exceptions::Generic',
267 'description' => "This kind of error happens when there is a system misconfiguration.",
268 },
269 'Bio::NEXUS::Util::Exceptions::FileError' => {
270 'isa' => 'Bio::NEXUS::Util::Exceptions::System',
271 'description' => "This kind of error happens when a file can not be accessed.",
272 },
273 'Bio::NEXUS::Util::Exceptions::ExtensionError' => {
274 'isa' => [
275 'Bio::NEXUS::Util::Exceptions::System',
276 'Bio::NEXUS::Util::Exceptions::BadFormat',
277 ],
278 'description' => "This kind of error happens when an extension module can not be\nloaded.",
279 },
280 'Bio::NEXUS::Util::Exceptions::BadFormat' => {
281 'isa' => 'Bio::NEXUS::Util::Exceptions::System',
282 'description' => "This kind of error happens when a bad\nparse or unparse format was specified.",
283 },
284  
285 );
286  
287  
288 35 1;
289 __END__
« lib/Bio/NEXUS/TaxaBlock.pm ↑ Summary