File Coverage

blib/lib/Bio/Phylo/Util/Exceptions.pm
Criterion Covered Total %
statement 167 187 89.3
branch 23 50 46.0
condition 0 3 0.0
subroutine 46 49 93.8
pod 5 5 100.0
total 241 294 81.9


line stmt bran cond sub pod time code
1             package Bio::Phylo::Util::Exceptions;
2 57     57   367 use strict;
  57         115  
  57         1532  
3 57     57   270 use base 'Exporter';
  57         100  
  57         5536  
4 57     57   14266 use Bio::Phylo::Util::StackTrace;
  57         142  
  57         1815  
5 57     57   329 use Scalar::Util 'blessed';
  57         103  
  57         4429  
6 57     57   47885 use overload 'bool' => sub { 1 }, 'fallback' => 1, '""' => \&as_string;
  57     208   46929  
  57         521  
  208         616  
7             our ( @EXPORT_OK, $AUTOLOAD ) = qw'throw';
8              
9             sub new {
10 176     176 1 335 my $class = shift;
11 176         600 my %args = @_;
12 176         995 my $self = {
13              
14             # 'error' => $args{'error'},
15             # 'description' => $args{'description'},
16             'trace' => Bio::Phylo::Util::StackTrace->new,
17             'time' => CORE::time(),
18             'pid' => $$,
19             'uid' => $<,
20             'euid' => $>,
21             'gid' => $(,
22             'egid' => $),
23             %args
24             };
25 176         763 return bless $self, $class;
26             }
27              
28             sub as_string {
29 0     0 1 0 my $self = shift;
30 0         0 my $error = $self->error;
31 0         0 my $description = $self->description;
32 0         0 my $class = ref $self;
33 0         0 my $trace = join "\n", map { "STACK: $_" } split '\n',
  0         0  
34             $self->trace->as_string;
35 0         0 return <<"ERROR_HERE_DOC";
36             -------------------------- EXCEPTION ----------------------------
37             Message: $error
38              
39             An exception of type $class
40             was thrown.
41              
42             $description
43              
44             Refer to the Bio::Phylo::Util::Exceptions documentation for more
45             information.
46             ------------------------- STACK TRACE ---------------------------
47             $trace
48             -----------------------------------------------------------------
49             ERROR_HERE_DOC
50             }
51              
52             sub throw (@) {
53              
54             # called as static method, with odd args
55 176     176 1 355 my $self;
56 176 50       599 if ( scalar @_ % 2 ) {
57 0         0 my $class = shift;
58 0         0 $self = $class->new(@_);
59             }
60              
61             # called as function, with even args e.g. throw BadArgs => 'msg';
62             else {
63 176         367 my $type = shift;
64 176         476 my $class = __PACKAGE__ . '::' . $type;
65 176 50       2036 if ( $class->isa('Bio::Phylo::Util::Exceptions') ) {
66 176         809 $self = $class->new( 'error' => shift, @_ );
67             }
68             else {
69 0         0 $self = Bio::Phylo::Util::Exceptions::Generic->new(
70             'error' => shift,
71             @_
72             );
73             }
74             }
75              
76             # if ( not $ENV{'PERL_DL_NONLAZY'} ) {
77             # require Bio::Phylo;
78             # $Bio::Phylo::Util::Logger::TRACEBACK = 1;
79             # my $logger = Bio::Phylo->get_logger();
80             # $logger->error($self->error);
81             # $Bio::Phylo::Util::Logger::TRACEBACK = 0;
82             # }
83 176         1323 die $self;
84             }
85              
86             sub rethrow {
87 0     0 1 0 my $self = shift;
88 0         0 die $self;
89             }
90              
91             sub caught {
92 0     0 1 0 my $class = shift;
93 0 0       0 if (@_) {
94 0         0 $class = shift;
95             }
96 0 0       0 if ($@) {
97 0 0 0     0 if ( blessed $@ and $@->isa($class) ) {
98 0         0 return $@;
99             }
100             else {
101 0         0 die $@;
102             }
103             }
104             }
105              
106             sub AUTOLOAD {
107 176     176   4092 my $self = shift;
108 176         361 my $field = $AUTOLOAD;
109 176         1038 $field =~ s/.*://;
110 176 50       7486 return if $field eq 'DESTROY';
111 0         0 return $self->{$field};
112             }
113              
114             sub _make_exceptions {
115 57     57   780 my $class = shift;
116 57         795 my $root = shift;
117 57         1370 my %exceptions = @_;
118 57         353 for my $exception ( keys %exceptions ) {
119 1026         2070 my $isa = $exceptions{$exception}->{'isa'};
120 1026 100       2818 my @isa = ref $isa ? @$isa : ($isa);
121 1026         1578 my $description = $exceptions{$exception}->{'description'};
122 1026         3121 my $class = <<"EXCEPTION_CLASS";
123             package ${exception};
124             use vars '\@ISA';
125             \@ISA=qw(@isa);
126             my \$desc;
127             sub description {
128             my \$self = shift;
129             if ( \@_ ) {
130             \$desc = shift;
131             }
132             return \$desc;
133             }
134             1;
135             EXCEPTION_CLASS
136 57 50   57   406 eval $class;
  57 50   57   109  
  57 50   57   4795  
  57 50   57   337  
  57 50   57   141  
  57 50   57   4086  
  57 50   57   365  
  57 50   57   108  
  57 50   57   3871  
  57 50   57   336  
  57 50   57   107  
  57 50   57   3914  
  57 50   57   332  
  57 50   57   115  
  57 50   57   3978  
  57 50   57   317  
  57 50   57   120  
  57 50   57   3810  
  57     57   328  
  57     57   119  
  57     57   5376  
  57     57   1147  
  57     57   709  
  57     57   3836  
  57     57   329  
  57     57   123  
  57     57   3742  
  57     57   317  
  57     57   108  
  57     57   3832  
  57     57   317  
  57     57   120  
  57     57   3769  
  57     57   322  
  57     57   114  
  57     57   3940  
  57         393  
  57         105  
  57         3910  
  57         331  
  57         113  
  57         3721  
  57         332  
  57         234  
  57         3740  
  57         341  
  57         129  
  57         4124  
  57         318  
  57         107  
  57         3739  
  57         312  
  57         138  
  57         3653  
  1026         67411  
  57         157  
  57         202  
  57         121  
  57         189  
  57         155  
  57         209  
  57         122  
  57         165  
  57         159  
  57         213  
  57         106  
  57         171  
  57         188  
  57         211  
  57         123  
  57         192  
  57         142  
  57         203  
  57         102  
  57         172  
  57         166  
  57         211  
  57         103  
  57         166  
  57         168  
  57         215  
  57         131  
  57         185  
  57         178  
  57         218  
  57         112  
  57         174  
  57         159  
  57         207  
  57         109  
  57         226  
  57         165  
  57         225  
  57         149  
  57         1010  
  57         178  
  57         220  
  57         129  
  57         189  
  57         164  
  57         204  
  57         124  
  57         180  
  57         184  
  57         228  
  57         119  
  57         176  
  57         174  
  57         217  
  57         120  
  57         173  
  57         163  
  57         219  
  57         107  
  57         190  
  57         202  
  57         209  
  57         115  
  57         937  
  57         184  
  57         237  
  57         119  
  57         191  
  57         153  
  57         204  
  57         118  
  57         169  
137 1026         24452 $exception->description($description);
138             }
139             }
140             __PACKAGE__->_make_exceptions(
141              
142             # root classes
143             'Bio::Phylo::Util::Exceptions',
144             'Bio::Phylo::Util::Exceptions::Generic' => {
145             'isa' => 'Bio::Phylo::Util::Exceptions',
146             'description' =>
147             "No further details about this type of error are available."
148             },
149              
150             # exceptions on method calls
151             'Bio::Phylo::Util::Exceptions::API' => {
152             'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
153             'description' =>
154             "No more details about this type of error are available."
155             },
156             'Bio::Phylo::Util::Exceptions::UnknownMethod' => {
157             'isa' => 'Bio::Phylo::Util::Exceptions::API',
158             'description' =>
159             "This kind of error happens when a non-existent method is called.",
160             },
161             'Bio::Phylo::Util::Exceptions::NotImplemented' => {
162             'isa' => 'Bio::Phylo::Util::Exceptions::API',
163             'description' =>
164             "This kind of error happens when a non-implemented\n(interface) method is called.",
165             },
166             'Bio::Phylo::Util::Exceptions::Deprecated' => {
167             'isa' => 'Bio::Phylo::Util::Exceptions::API',
168             'description' =>
169             "This kind of error happens when a deprecated method is called.",
170             },
171              
172             # exceptions on arguments
173             'Bio::Phylo::Util::Exceptions::BadArgs' => {
174             'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
175             'description' =>
176             "This kind of error happens when bad or incomplete arguments\nare provided.",
177             },
178             'Bio::Phylo::Util::Exceptions::BadString' => {
179             'isa' => 'Bio::Phylo::Util::Exceptions::BadArgs',
180             'description' =>
181             "This kind of error happens when an unsafe string argument is\nprovided.",
182             },
183             'Bio::Phylo::Util::Exceptions::OddHash' => {
184             'isa' => 'Bio::Phylo::Util::Exceptions::BadArgs',
185             'description' =>
186             "This kind of error happens when an uneven number\nof arguments (so no key/value pairs) was provided.",
187             },
188             'Bio::Phylo::Util::Exceptions::ObjectMismatch' => {
189             'isa' => 'Bio::Phylo::Util::Exceptions::BadArgs',
190             'description' =>
191             "This kind of error happens when an invalid object\nargument is provided.",
192             },
193             'Bio::Phylo::Util::Exceptions::InvalidData' => {
194             'isa' => [
195             'Bio::Phylo::Util::Exceptions::BadString',
196             'Bio::Phylo::Util::Exceptions::BadFormat',
197             ],
198             'description' =>
199             "This kind of error happens when invalid character data is\nprovided."
200             },
201             'Bio::Phylo::Util::Exceptions::OutOfBounds' => {
202             'isa' => 'Bio::Phylo::Util::Exceptions::BadArgs',
203             'description' =>
204             "This kind of error happens when an index is outside of its range.",
205             },
206             'Bio::Phylo::Util::Exceptions::BadNumber' => {
207             'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
208             'description' =>
209             "This kind of error happens when an invalid numerical argument\nis provided.",
210             },
211             'Bio::Phylo::Util::Exceptions::NoData' => {
212             'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
213             'description' =>
214             "This kind of error happens when a data source is empty.",
215             },
216              
217             # system exceptions
218             'Bio::Phylo::Util::Exceptions::System' => {
219             'isa' => 'Bio::Phylo::Util::Exceptions::Generic',
220             'description' =>
221             "This kind of error happens when there is a system misconfiguration.",
222             },
223             'Bio::Phylo::Util::Exceptions::FileError' => {
224             'isa' => 'Bio::Phylo::Util::Exceptions::System',
225             'description' =>
226             "This kind of error happens when a file can not be accessed.",
227             },
228             'Bio::Phylo::Util::Exceptions::NetworkError' => {
229             'isa' => 'Bio::Phylo::Util::Exceptions::System',
230             'description' =>
231             "This kind of error happens when a network resource can not be accessed.",
232             },
233             'Bio::Phylo::Util::Exceptions::ExtensionError' => {
234             'isa' => [
235             'Bio::Phylo::Util::Exceptions::System',
236             'Bio::Phylo::Util::Exceptions::BadFormat',
237             ],
238             'description' =>
239             "This kind of error happens when an extension module can not be\nloaded.",
240             },
241             'Bio::Phylo::Util::Exceptions::BadFormat' => {
242             'isa' => 'Bio::Phylo::Util::Exceptions::System',
243             'description' =>
244             "This kind of error happens when a bad\nparse or unparse format was specified.",
245             },
246             );
247             1;
248             __END__