EnsEMBL/Utils/Exception.pm untested

filter: Statementstmt 0.0% 0 / 141 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 29 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 255 CC 32 SCAR 38.3
CC
32
Cov
0%
verbose
110.0
warning
56.0
deprecate
42.0
CRAP 46.2
1 =head1 LICENSE
2  
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5  
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9  
10 http://www.apache.org/licenses/LICENSE-2.0
11  
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17  
18 =cut
19  
20  
21 =head1 CONTACT
22  
23 Please email comments or questions to the public Ensembl
24 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25  
26 Questions may also be sent to the Ensembl help desk at
27 <http://www.ensembl.org/Help/Contact>.
28  
29 =cut
30  
31 =head1 NAME
32  
33 Bio::EnsEMBL::Utils::Exception - Utility functions for error handling
34  
35 =head1 SYNOPSIS
36  
37 use Bio::EnsEMBL::Utils::Exception
38 qw(throw warning deprecate verbose try catch);
39  
40 or to get all methods just
41  
42 use Bio::EnsEMBL::Utils::Exception;
43  
44 eval { throw("this is an exception with a stack trace") };
45 if ($@) {
46 print "Caught exception:\n$@";
47 }
48  
49 # Or you can us the try/catch confortable syntax instead to deal with
50 # throw or die. Don't forget the ";" after the catch block. With
51 # this syntax, the original $@ is in $_ in the catch subroutine.
52  
53 try {
54 throw("this is an exception with a stack trace");
55 }
56 catch { print "Caught exception:\n$_" };
57  
58 # silence warnings
59 verbose('OFF');
60  
61 warning('this is a silent warning');
62  
63 #show deprecated and warning messages but not info
64 verbose('DEPRECATE');
65  
66 warning('this is a warning');
67  
68 # show all messages
69 verbose('ALL');
70  
71 info('this is an informational message');
72  
73 sub my_sub { deprecate('use other_sub() instead') }
74  
75 verbose('EXCEPTION');
76 info( 'This is a high priority info message.', 1000 );
77  
78 =head1 DESCRIPTION
79  
80 This is derived from the Bio::Root module in BioPerl. Some formatting
81 has been changed and the deprecate function has been added. Most
82 notably the object methods are now static class methods that can be
83 called without inheriting from Bio::Root. This is
84 especially useful for throwing exceptions with stack traces outside of a
85 blessed context.
86  
87 The originaly implementations of these methods were by Steve Chervitz
88 and refactored by Ewan Birney.
89  
90 It is recommended that these functions be used instead of inheriting
91 unnecessarily from the Bio::Root object. The
92 functions exported by this package provide a set of useful error
93 handling methods.
94  
95 =head1 METHODS
96  
97 =cut
98  
99 package Bio::EnsEMBL::Utils::Exception;
100 $Bio::EnsEMBL::Utils::Exception::VERSION = '114.0.0_50'; # TRIAL
101 $Bio::EnsEMBL::Utils::Exception::VERSION = '114.0.050';
102 use strict;
103 use warnings;
104  
105 use Bio::EnsEMBL::ApiVersion;
106  
107 use Exporter;
108  
109 use vars qw(@ISA @EXPORT);
110  
111 @ISA = qw(Exporter);
112 @EXPORT = qw(throw warning stack_trace_dump
113 stack_trace verbose deprecate info try catch);
114  
115 my $VERBOSITY = 3000;
116 my $DEFAULT_INFO = 4000;
117 my $DEFAULT_DEPRECATE = 3000;
118 my $DEFAULT_WARNING = 2000;
119 my $DEFAULT_EXCEPTION = 1000;
120  
121  
122 =head2 throw
123  
124 Arg [1] : string $msg
125 Arg [2] : (optional) int $level
126 override the default level of exception throwing
127 Example : use Bio::EnsEMBL::Utils::Exception qw(throw);
128 throw('We have a problem');
129 Description: Throws an exception which if not caught by an eval will
130 provide a stack trace to STDERR and die. If the verbosity level
131 is lower than the level of the throw, then no error message is
132 displayed but the program will still die (unless the exception
133 is caught).
134 Returntype : none
135 Exceptions : thrown every time
136 Caller : generally on error
137  
138 =cut
139  
140 sub throw {
141 my $string = shift;
142  
143 # For backwards compatibility with Bio::EnsEMBL::Root::throw: Allow
144 # to be called as an object method as well as class method. Root
145 # function now deprecated so call will have the string instead.
146  
147 $string = shift if ( ref($string) ); # Skip object if one provided.
148 $string = shift if ( $string eq "Bio::EnsEMBL::Utils::Exception" );
149  
150 my $level = shift;
151 $level = $DEFAULT_EXCEPTION if ( !defined($level) );
152  
153 if ( $VERBOSITY < $level ) {
154 die("\n"); # still die, but silently
155 }
156  
157 my $std = stack_trace_dump(3);
158  
159 my $out = sprintf(
160 "\n" .
161 "-------------------- EXCEPTION --------------------\n" .
162 "MSG: %s\n" .
163 "%s" .
164 "Date (localtime) = %s\n" .
165 "Ensembl API version = %s\n" .
166 "---------------------------------------------------\n",
167 $string, $std, scalar( localtime() ), software_version() );
168  
169 die($out);
170 } ## end sub throw
171  
172  
173  
174 =head2 warning
175  
176 Arg [1] : string warning(message);
177 Arg [2] : (optional) int level
178 Override the default level of this warning changning the level
179 of verbosity at which it is displayed.
180 Example : use Bio::EnsEMBL::Utils::Exception qw(warning)
181 warning('This is a warning');
182 Description: If the verbosity level is higher or equal to the level of this
183 warning then a warning message is printed to STDERR. If the
184 verbosity lower then nothing is done. Under the default
185 levels of warning and verbosity warnings will be displayed.
186 Returntype : none
187 Exceptions : warning every time
188 Caller : general
189  
190 =cut
191  
192 sub warning {
193 my $string = shift;
194  
195 # See throw() for this:
196 $string = shift if ( ref($string) ); # Skip object if one provided.
197 $string = shift if ( $string eq "Bio::EnsEMBL::Utils::Exception" );
198  
199 my $level = shift;
200 $level = $DEFAULT_WARNING if ( !defined($level) );
201  
202 return if ( $VERBOSITY < $level );
203  
204 my @caller = caller;
205 my $line = $caller[2] || '';
206  
207 # Use only two sub-dirs for brevity when reporting the file name.
208 my $file;
209 my @path = split( /\//, $caller[1] );
210 $file = pop(@path);
211 my $i = 0;
212 while ( @path && $i < 2 ) {
213 $i++;
214 $file = pop(@path) . "/$file";
215 }
216  
217 @caller = caller(1);
218 my $caller_line;
219 my $caller_file;
220 $i = 0;
221 if (@caller) {
222 @path = split( /\//, $caller[1] );
223 $caller_line = $caller[2];
224 $caller_file = pop(@path);
225 while ( @path && $i < 2 ) {
226 $i++;
227 $caller_file = pop(@path) . "/$caller_file";
228 }
229 }
230  
231 my $out =
232 sprintf( "\n" .
233 "-------------------- WARNING ----------------------\n" .
234 "MSG: %s\n" .
235 "FILE: %s LINE: %d\n",
236 $string, $file, $line );
237  
238 if ( defined($caller_file) ) {
239 $out .= sprintf( "CALLED BY: %s LINE: %d\n", $caller_file,
240 $caller_line );
241 }
242 $out .= sprintf(
243 "Date (localtime) = %s\n" .
244 "Ensembl API version = %s\n" .
245 "---------------------------------------------------\n",
246 scalar( localtime() ), software_version() );
247  
248 warn($out);
249  
250 } ## end sub warning
251  
252  
253  
254 =head2 info
255  
256 Arg [1] : string $string
257 The message to be displayed
258 Arg [2] : (optional) int $level
259 Override the default level of this message so it is displayed at
260 a different level of verbosity than it normally would be.
261 Example : use Bio::EnsEMBL::Utils::Exception qw(verbose info)
262 Description: This prints an info message to STDERR if verbosity is higher
263 than the level of the message. By default info messages are not
264 displayed.
265 Returntype : none
266 Exceptions : none
267 Caller : general
268  
269 =cut
270  
271 sub info {
272 my $string = shift;
273 $string = shift if($string eq "Bio::EnsEMBL::Utils::Exception");
274 my $level = shift;
275  
276 $level = $DEFAULT_INFO if(!defined($level));
277  
278 return if($VERBOSITY < $level);
279  
280 print STDERR "INFO: $string\n";
281 }
282  
283  
284  
285 =head2 verbose
286  
287 Arg [1] : (optional) int
288 Example : use Bio::EnsEMBL::Utils::Exception qw(verbose warning);
289 #turn warnings and everything more important on (e.g. exception)
290 verbose('WARNING');
291 warning("Warning displayed");
292 info("This won't be displayed");
293 deprecate("This won't be diplayed");
294  
295 #turn exception messages on
296 verbose('EXCEPTION');
297 warning("This won't do anything");
298 throw("Die with a message");
299  
300 #turn everying off
301 verbose('OFF'); #same as verbose(0);
302 warning("This won't do anything");
303 throw("Die silently without a message");
304  
305 #turn on all messages
306 verbose('ALL');
307 info("All messages are now displayed");
308  
309 if(verbose() > 3000) {
310 print "Verbosity is pretty high";
311 }
312  
313 Description: Gets/Sets verbosity level which defines which messages are
314 to be displayed. An integer value may be passed or one of the
315 following strings:
316 'OFF' (= 0)
317 'EXCEPTION' (= 1000)
318 'WARNING' (= 2000)
319 'DEPRECATE' (= 3000)
320 'INFO' (= 4000)
321 'ALL' (= 1000000)
322  
323 Returntype : int
324 Exceptions : none
325 Caller : general
326  
327 =cut
328  
329  
330 sub verbose {
331 if(@_) {
332 my $verbosity = shift;
333 $verbosity = shift if($verbosity eq "Bio::EnsEMBL::Utils::Exception");
334 if($verbosity =~ /\d+/) { #check if verbosity is an integer
335 $VERBOSITY = $verbosity;
336 } else {
337 $verbosity = uc($verbosity);
338 if($verbosity eq 'OFF' || $verbosity eq 'NOTHING' ||
339 $verbosity eq 'NONE') {
340 $VERBOSITY = 0;
341 } elsif($verbosity eq 'EXCEPTION' || $verbosity eq 'THROW') {
342 $VERBOSITY = $DEFAULT_EXCEPTION;
343 } elsif($verbosity eq 'WARNING' || $verbosity eq 'WARN') {
344 $VERBOSITY = $DEFAULT_WARNING;
345 } elsif($verbosity eq 'DEPRECATE' || $verbosity eq 'DEPRECATED') {
346 $VERBOSITY = $DEFAULT_DEPRECATE;
347 } elsif($verbosity eq 'INFO') {
348 $VERBOSITY = $DEFAULT_INFO;
349 } elsif($verbosity eq 'ON' || $verbosity eq 'ALL') {
350 $VERBOSITY = 1e6;
351 } else {
352 $VERBOSITY = $DEFAULT_WARNING;
353 warning("Unknown level of verbosity: $verbosity");
354 }
355 }
356 }
357  
358 return $VERBOSITY;
359 }
360  
361  
362  
363 =head2 stack_trace_dump
364  
365 Arg [1] : (optional) int $levels
366 The number of levels to ignore from the top of the stack when
367 creating the dump. This is useful when this is called internally
368 from a warning or throw function when the immediate caller and
369 stack_trace_dump function calls are themselves uninteresting.
370 Example : use Bio::EnsEMBL::Utils::Exception qw(stack_trace_dump);
371 print STDERR stack_trace_dump();
372 Description: Returns a stack trace formatted as a string
373 Returntype : string
374 Exceptions : none
375 Caller : general, throw, warning
376  
377 =cut
378  
379 sub stack_trace_dump{
380 my @stack = stack_trace();
381  
382 my $levels = 2; #default is 2 levels so stack_trace_dump call is not present
383 $levels = shift if(@_);
384 $levels = shift if($levels eq "Bio::EnsEMBL::Utils::Exception");
385 $levels = 1 if($levels < 1);
386
387 while($levels) {
388 $levels--;
389 shift @stack;
390 }
391  
392 my $out;
393 my ($module,$function,$file,$position);
394  
395  
396 foreach my $stack ( @stack) {
397 ($module,$file,$position,$function) = @{$stack};
398 $out .= "STACK $function $file:$position\n";
399 }
400  
401 return $out;
402 }
403  
404  
405  
406 =head2 stack_trace
407  
408 Arg [1] : none
409 Example : use Bio::EnsEMBL::Utils::Exception qw(stack_trace)
410 Description: Gives an array to a reference of arrays with stack trace info
411 each coming from the caller(stack_number) call
412 Returntype : array of listrefs of strings
413 Exceptions : none
414 Caller : general, stack_trace_dump()
415  
416 =cut
417  
418 sub stack_trace {
419 my $i = 0;
420 my @out;
421 my $prev;
422 while ( my @call = caller($i++)) {
423  
424 # major annoyance that caller puts caller context as
425 # function name. Hence some monkeying around...
426 $prev->[3] = $call[3];
427 push(@out,$prev);
428 $prev = \@call;
429 }
430 $prev->[3] = 'toplevel';
431 push(@out,$prev);
432 return @out;
433 }
434  
435  
436 =head2 deprecate
437  
438 Arg [1] : string $mesg
439 A message describing why a method is deprecated
440 Example : use Bio::EnsEMBL::Utils::Exception qw(deprecate)
441 sub old_sub {
442 deprecate('Please use new_sub() instead');
443 }
444 Description: Prints a warning to STDERR that the method which called
445 deprecate() is deprecated. Also prints the line number and
446 file from which the deprecated method was called. Deprecated
447 warnings only appear once for each location the method was
448 called from. No message is displayed if the level of verbosity
449 is lower than the level of the warning.
450 Returntype : none
451 Exceptions : warning every time
452 Caller : deprecated methods
453  
454 =cut
455  
456 my %DEPRECATED;
457  
458 sub deprecate {
459 my $mesg = shift;
460 $mesg = shift if($mesg eq "Bio::EnsEMBL::Utils::Exception"); #skip object if one provided
461  
462 my $level = shift;
463  
464 $level = $DEFAULT_DEPRECATE if(!defined($level));
465  
466 return if($VERBOSITY < $level);
467
468 my @caller = caller(1);
469 my $subname = $caller[3] ;
470 my $line = $caller[2];
471  
472 #use only 2 subdirs for brevity when reporting the filename
473 my $file;
474 my @path = $caller[1];
475 $file = pop(@path);
476 my $i = 0;
477 while(@path && $i < 2) {
478 $i++;
479 $file .= pop(@path);
480 }
481  
482 #keep track of who called this method so that the warning is only displayed
483 #once per deprecated call
484 return if $DEPRECATED{"$line:$file:$subname"};
485  
486 if ( $VERBOSITY > -1 ) {
487 print STDERR
488 "\n------------------ DEPRECATED ---------------------\n"
489 . "Deprecated method call in file $file line $line.\n"
490 . "Method $subname is deprecated.\n"
491 . "$mesg\n"
492 . "Ensembl API version = "
493 . software_version() . "\n"
494 . "---------------------------------------------------\n";
495 }
496  
497 $DEPRECATED{"$line:$file:$subname"} = 1;
498 }
499  
500 =head2 try/catch
501  
502 Arg [1] : anonymous subroutine
503 the block to be tried
504 Arg [2] : return value of the catch function
505 Example : use Bio::EnsEMBL::Utils::Exception qw(throw try catch)
506 The syntax is:
507 try { block1 } catch { block2 };
508 { block1 } is the 1st argument
509 catch { block2 } is the 2nd argument
510 e.g.
511 try {
512 throw("this is an exception with a stack trace");
513 } catch {
514 print "Caught exception:\n$_";
515 };
516 In block2, $_ is assigned the value of the first
517 throw or die statement executed in block 1.
518  
519 Description: Replaces the classical syntax
520 eval { block1 };
521 if ($@) { block2 }
522 by a more confortable one.
523 In the try/catch syntax, the original $@ is in $_ in the catch subroutine.
524 This try/catch implementation is a copy and paste from
525 "Programming Perl" 3rd Edition, July 2000, by L.Wall, T. Christiansen
526 & J. Orwant. p227, and is only possible because of subroutine prototypes.
527 Returntype : depend on what is implemented the try or catch block
528 Exceptions : none
529 Caller : general
530  
531 =cut
532  
533 ## no critic
534 sub try (&$) {
535 my ($try, $catch) = @_;
536 eval { &$try };
537 if ($@) {
538 chop $@;
539 local $_ = $@;
540 &$catch;
541 }
542 }
543  
544 ## no critic
545 sub catch (&) {
546 shift;
547 }
548  
549 1;