Phylo/Util/Exceptions.pm

filter: Statementstmt 89.6% 173 / 193 Branchbran 46.0% 23 / 50 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.0% 47 / 50 Podpod 100% 5 / 5 totaltotal 81.8% 248 / 303 CC 28 SCAR 12.8
CC
28
Cov
80%
caught
30.0
throw
3.4
_make_exceptions
3.0
CRAP 3.6
« Phylo/Matrices/Datatype/Custom.pm ↑ Summary Phylo/NeXML/DOM.pm »
1 package Bio::Phylo::Util::Exceptions;
2 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 57 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use Bio::Phylo::Util::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 57 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 57 ▶use overload 'bool' => sub { 1 }, 'fallback' => 1, '""' => \&as_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 57 our ( @EXPORT_OK, $AUTOLOAD ) = qw'throw';
9  
10 176 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
11 176 my $class = shift;
12 176 my %args = @_;
13 176 my $self = {
14  
15 # 'error' => $args{'error'},
16 # 'description' => $args{'description'},
17 'trace' => Bio::Phylo::Util::StackTrace->new,
18 'time' => CORE::time(),
19 'pid' => $$,
20 'uid' => $<,
21 'euid' => $>,
22 'gid' => $(,
23 'egid' => $),
24 %args
25 };
26 176 return bless $self, $class;
27 }
28  
29 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
30 0 my $self = shift;
31 0 my $error = $self->error;
32 0 my $description = $self->description;
33 0 my $class = ref $self;
34 0 my $trace = join "\n", map { "STACK: $_" } split '\n',
35 $self->trace->as_string;
36 0 return <<"ERROR_HERE_DOC";
37 -------------------------- EXCEPTION ----------------------------
38 Message: $error
39  
40 An exception of type $class
41 was thrown.
42  
43 $description
44  
45 Refer to the Bio::Phylo::Util::Exceptions documentation for more
46 information.
47 ------------------------- STACK TRACE ---------------------------
48 $trace
49 -----------------------------------------------------------------
50 ERROR_HERE_DOC
51 }
52  
53 176 ▶sub throw (@) {
Subroutine throw: called CC=3 SCAR=12.3
Pod documented
54  
55 # called as static method, with odd args
56 176 my $self;
57 176 ▶ if ( scalar @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ % 2) { } 0 176 176
58 0 my $class = shift;
59 0 $self = $class->new(@_);
60 }
61  
62 # called as function, with even args e.g. throw BadArgs => 'msg';
63 else {
64 176 my $type = shift;
65 176 my $class = __PACKAGE__ . '::' . $type;
66 176 ▶ if ( $class->isa('Bio::Phylo::Util::Exceptions') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->isa('Bio::Phylo::Util::Exceptions')) { } 176 0 176
67 176 $self = $class->new( 'error' => shift, @_ );
68 }
69 else {
70 0 $self = Bio::Phylo::Util::Exceptions::Generic->new(
71 'error' => shift,
72 @_
73 );
74 }
75 }
76  
77 # if ( not $ENV{'PERL_DL_NONLAZY'} ) {
78 # require Bio::Phylo;
79 # $Bio::Phylo::Util::Logger::TRACEBACK = 1;
80 # my $logger = Bio::Phylo->get_logger();
81 # $logger->error($self->error);
82 # $Bio::Phylo::Util::Logger::TRACEBACK = 0;
83 # }
84 176 die $self;
85 }
86  
87 0 ▶sub rethrow {
Subroutine rethrow: not called CC=1 SCAR=6.9
Pod documented
88 0 my $self = shift;
89 0 die $self;
90 }
91  
92 0 ▶sub caught {
Subroutine caught: not called CC=5 SCAR=34.0
Pod documented
93 0 my $class = shift;
94 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
95 0 $class = shift;
96 }
97 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
98 0 ▶ if ( blessed $@ and $@->isa($class) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->isa($class)) { } 0 0 0
Condition0%0/3
builtin::blessed($@) and $@->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($@) and $@->isa($class)
builtin::blessed($@) $@->isa($class)
Truth table0%0/3
A and B
A= builtin::blessed($@)B= $@->isa($class)
ABresult
0X0
100
111
99 0 return $@;
100 }
101 else {
102 0 die $@;
103 }
104 }
105 }
106  
107 176 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.4
108 176 my $self = shift;
109 176 my $field = $AUTOLOAD;
110 176 $field =~ s/.*://;
111 176 ▶ return if $field eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "DESTROY" 176 0 176
112 0 return $self->{$field};
113 }
114  
115 57 ▶sub _make_exceptions {
Subroutine _make_exceptions: called CC=3 SCAR=11.0
116 57 my $class = shift;
117 57 my $root = shift;
118 57 my %exceptions = @_;
119 57 for my $exception ( keys %exceptions ) {
120 1026 my $isa = $exceptions{$exception}->{'isa'};
121 1026 ▶ my @isa = ref $isa ? @$isa : ($isa);
Branch: 1 branch
BranchTrueFalseTotal
ref $isa ? : 114 912 1026
122 1026 my $description = $exceptions{$exception}->{'description'};
123 1026 my $class = <<"EXCEPTION_CLASS";
124 package ${exception};
125 use vars '\@ISA';
126 \@ISA=qw(@isa);
127 my \$desc;
128 sub description {
129 my \$self = shift;
130 if ( \@_ ) {
131 \$desc = shift;
132 }
133 return \$desc;
134 }
135 1;
136 EXCEPTION_CLASS
137 57 ▶ 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 BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Subroutine description: called CC=2 SCAR=7.0
Branch: 18 branches
BranchTrueFalseTotal
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
if (@_) 57 0 57
138 1026 $exception->description($description);
139 }
140 }
141 57 __PACKAGE__->_make_exceptions(
142  
143 # root classes
144 'Bio::Phylo::Util::Exceptions',
145 'Bio::Phylo::Util::Exceptions::Generic' => {
146 'isa' => 'Bio::Phylo::Util::Exceptions',
147 'description' =>
148 "No further details about this type of error are available."
149 },
150  
151 # exceptions on method calls
152 'Bio::Phylo::Util::Exceptions::API' => {
153 'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
154 'description' =>
155 "No more details about this type of error are available."
156 },
157 'Bio::Phylo::Util::Exceptions::UnknownMethod' => {
158 'isa' => 'Bio::Phylo::Util::Exceptions::API',
159 'description' =>
160 "This kind of error happens when a non-existent method is called.",
161 },
162 'Bio::Phylo::Util::Exceptions::NotImplemented' => {
163 'isa' => 'Bio::Phylo::Util::Exceptions::API',
164 'description' =>
165 "This kind of error happens when a non-implemented\n(interface) method is called.",
166 },
167 'Bio::Phylo::Util::Exceptions::Deprecated' => {
168 'isa' => 'Bio::Phylo::Util::Exceptions::API',
169 'description' =>
170 "This kind of error happens when a deprecated method is called.",
171 },
172  
173 # exceptions on arguments
174 'Bio::Phylo::Util::Exceptions::BadArgs' => {
175 'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
176 'description' =>
177 "This kind of error happens when bad or incomplete arguments\nare provided.",
178 },
179 'Bio::Phylo::Util::Exceptions::BadString' => {
180 'isa' => 'Bio::Phylo::Util::Exceptions::BadArgs',
181 'description' =>
182 "This kind of error happens when an unsafe string argument is\nprovided.",
183 },
184 'Bio::Phylo::Util::Exceptions::OddHash' => {
185 'isa' => 'Bio::Phylo::Util::Exceptions::BadArgs',
186 'description' =>
187 "This kind of error happens when an uneven number\nof arguments (so no key/value pairs) was provided.",
188 },
189 'Bio::Phylo::Util::Exceptions::ObjectMismatch' => {
190 'isa' => 'Bio::Phylo::Util::Exceptions::BadArgs',
191 'description' =>
192 "This kind of error happens when an invalid object\nargument is provided.",
193 },
194 'Bio::Phylo::Util::Exceptions::InvalidData' => {
195 'isa' => [
196 'Bio::Phylo::Util::Exceptions::BadString',
197 'Bio::Phylo::Util::Exceptions::BadFormat',
198 ],
199 'description' =>
200 "This kind of error happens when invalid character data is\nprovided."
201 },
202 'Bio::Phylo::Util::Exceptions::OutOfBounds' => {
203 'isa' => 'Bio::Phylo::Util::Exceptions::BadArgs',
204 'description' =>
205 "This kind of error happens when an index is outside of its range.",
206 },
207 'Bio::Phylo::Util::Exceptions::BadNumber' => {
208 'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
209 'description' =>
210 "This kind of error happens when an invalid numerical argument\nis provided.",
211 },
212 'Bio::Phylo::Util::Exceptions::NoData' => {
213 'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
214 'description' =>
215 "This kind of error happens when a data source is empty.",
216 },
217  
218 # system exceptions
219 'Bio::Phylo::Util::Exceptions::System' => {
220 'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
221 'description' =>
222 "This kind of error happens when there is a system misconfiguration.",
223 },
224 'Bio::Phylo::Util::Exceptions::FileError' => {
225 'isa' => 'Bio::Phylo::Util::Exceptions::System',
226 'description' =>
227 "This kind of error happens when a file can not be accessed.",
228 },
229 'Bio::Phylo::Util::Exceptions::NetworkError' => {
230 'isa' => 'Bio::Phylo::Util::Exceptions::System',
231 'description' =>
232 "This kind of error happens when a network resource can not be accessed.",
233 },
234 'Bio::Phylo::Util::Exceptions::ExtensionError' => {
235 'isa' => [
236 'Bio::Phylo::Util::Exceptions::System',
237 'Bio::Phylo::Util::Exceptions::BadFormat',
238 ],
239 'description' =>
240 "This kind of error happens when an extension module can not be\nloaded.",
241 },
242 'Bio::Phylo::Util::Exceptions::BadFormat' => {
243 'isa' => 'Bio::Phylo::Util::Exceptions::System',
244 'description' =>
245 "This kind of error happens when a bad\nparse or unparse format was specified.",
246 },
247 );
248 57 1;
249 __END__
« Phylo/Matrices/Datatype/Custom.pm ↑ Summary Phylo/NeXML/DOM.pm »