PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/t/lib/Error.pm

http://github.com/bioperl/bioperl-live
Perl | 741 lines | 539 code | 144 blank | 58 comment | 45 complexity | c814f5ead6450d7a277377f36f5d0cd6 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. # Error.pm
  2. #
  3. # Copyright (c) 1997-8 Graham Barr <gbarr@ti.com>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. #
  7. # Based on my original Error.pm, and Exceptions.pm by Peter Seibel
  8. # <peter@weblogic.com> and adapted by Jesse Glick <jglick@sig.bsh.com>.
  9. #
  10. # but modified ***significantly***
  11. package Error;
  12. use strict;
  13. use 5.004;
  14. use overload (
  15. '""' => 'stringify',
  16. '0+' => 'value',
  17. 'bool' => sub { return 1; },
  18. 'fallback' => 1
  19. );
  20. $Error::Depth = 0; # Depth to pass to caller()
  21. $Error::Debug = 0; # Generate verbose stack traces
  22. @Error::STACK = (); # Clause stack for try
  23. $Error::THROWN = undef; # last error thrown, a workaround until die $ref works
  24. my $LAST; # Last error created
  25. my %ERROR; # Last error associated with package
  26. # Exported subs are defined in Error::subs
  27. sub import {
  28. shift;
  29. local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
  30. Error::subs->import(@_);
  31. }
  32. # I really want to use last for the name of this method, but it is a keyword
  33. # which prevent the syntax last Error
  34. sub prior {
  35. shift; # ignore
  36. return $LAST unless @_;
  37. my $pkg = shift;
  38. return exists $ERROR{$pkg} ? $ERROR{$pkg} : undef
  39. unless ref($pkg);
  40. my $obj = $pkg;
  41. my $err = undef;
  42. if($obj->isa('HASH')) {
  43. $err = $obj->{'__Error__'}
  44. if exists $obj->{'__Error__'};
  45. }
  46. elsif($obj->isa('GLOB')) {
  47. $err = ${*$obj}{'__Error__'}
  48. if exists ${*$obj}{'__Error__'};
  49. }
  50. $err;
  51. }
  52. # Return as much information as possible about where the error
  53. # happened. The -stacktrace element only exists if $Error::DEBUG
  54. # was set when the error was created
  55. sub stacktrace {
  56. my $self = shift;
  57. return $self->{'-stacktrace'}
  58. if exists $self->{'-stacktrace'};
  59. my $text = exists $self->{'-text'} ? $self->{'-text'} : "Died";
  60. $text .= sprintf(" at %s line %d.\n", $self->file, $self->line)
  61. unless($text =~ /\n$/s);
  62. $text;
  63. }
  64. # Allow error propagation, ie
  65. #
  66. # $ber->encode(...) or
  67. # return Error->prior($ber)->associate($ldap);
  68. sub associate {
  69. my $err = shift;
  70. my $obj = shift;
  71. return unless ref($obj);
  72. if($obj->isa('HASH')) {
  73. $obj->{'__Error__'} = $err;
  74. }
  75. elsif($obj->isa('GLOB')) {
  76. ${*$obj}{'__Error__'} = $err;
  77. }
  78. $obj = ref($obj);
  79. $ERROR{ ref($obj) } = $err;
  80. return;
  81. }
  82. sub new {
  83. my $self = shift;
  84. my($pkg,$file,$line) = caller($Error::Depth);
  85. my $err = bless {
  86. '-package' => $pkg,
  87. '-file' => $file,
  88. '-line' => $line,
  89. @_
  90. }, $self;
  91. $err->associate($err->{'-object'})
  92. if(exists $err->{'-object'});
  93. # To always create a stacktrace would be very inefficient, so
  94. # we only do it if $Error::Debug is set
  95. if($Error::Debug) {
  96. require Carp;
  97. local $Carp::CarpLevel = $Error::Depth;
  98. my $text = defined($err->{'-text'}) ? $err->{'-text'} : "Error";
  99. my $trace = Carp::longmess($text);
  100. # Remove try calls from the trace
  101. $trace =~ s/(\n\s+\S+__ANON__[^\n]+)?\n\s+eval[^\n]+\n\s+Error::subs::try[^\n]+(?=\n)//sog;
  102. $trace =~ s/(\n\s+\S+__ANON__[^\n]+)?\n\s+eval[^\n]+\n\s+Error::subs::run_clauses[^\n]+\n\s+Error::subs::try[^\n]+(?=\n)//sog;
  103. $err->{'-stacktrace'} = $trace
  104. }
  105. $@ = $LAST = $ERROR{$pkg} = $err;
  106. }
  107. # Throw an error. this contains some very gory code.
  108. sub throw {
  109. my $self = shift;
  110. local $Error::Depth = $Error::Depth + 1;
  111. # if we are not rethrow-ing then create the object to throw
  112. $self = $self->new(@_) unless ref($self);
  113. die $Error::THROWN = $self;
  114. }
  115. # syntactic sugar for
  116. #
  117. # die with Error( ... );
  118. sub with {
  119. my $self = shift;
  120. local $Error::Depth = $Error::Depth + 1;
  121. $self->new(@_);
  122. }
  123. # syntactic sugar for
  124. #
  125. # record Error( ... ) and return;
  126. sub record {
  127. my $self = shift;
  128. local $Error::Depth = $Error::Depth + 1;
  129. $self->new(@_);
  130. }
  131. # catch clause for
  132. #
  133. # try { ... } catch CLASS with { ... }
  134. sub catch {
  135. my $pkg = shift;
  136. my $code = shift;
  137. my $clauses = shift || {};
  138. my $catch = $clauses->{'catch'} ||= [];
  139. unshift @$catch, $pkg, $code;
  140. $clauses;
  141. }
  142. # Object query methods
  143. sub object {
  144. my $self = shift;
  145. exists $self->{'-object'} ? $self->{'-object'} : undef;
  146. }
  147. sub file {
  148. my $self = shift;
  149. exists $self->{'-file'} ? $self->{'-file'} : undef;
  150. }
  151. sub line {
  152. my $self = shift;
  153. exists $self->{'-line'} ? $self->{'-line'} : undef;
  154. }
  155. sub text {
  156. my $self = shift;
  157. exists $self->{'-text'} ? $self->{'-text'} : undef;
  158. }
  159. # overload methods
  160. sub stringify {
  161. my $self = shift;
  162. defined $self->{'-text'} ? $self->{'-text'} : "Died";
  163. }
  164. sub value {
  165. my $self = shift;
  166. exists $self->{'-value'} ? $self->{'-value'} : undef;
  167. }
  168. package Error::Simple;
  169. @Error::Simple::ISA = qw(Error);
  170. sub new {
  171. my $self = shift;
  172. my $text = "" . shift;
  173. my $value = shift;
  174. my(@args) = ();
  175. local $Error::Depth = $Error::Depth + 1;
  176. @args = ( -file => $1, -line => $2)
  177. if($text =~ s/ at (\S+) line (\d+)(\.\n)?$//s);
  178. push(@args, '-value', 0 + $value)
  179. if defined($value);
  180. $self->SUPER::new(-text => $text, @args);
  181. }
  182. sub stringify {
  183. my $self = shift;
  184. my $text = $self->SUPER::stringify;
  185. $text .= sprintf(" at %s line %d.\n", $self->file, $self->line)
  186. unless($text =~ /\n$/s);
  187. $text;
  188. }
  189. ##########################################################################
  190. ##########################################################################
  191. # Inspired by code from Jesse Glick <jglick@sig.bsh.com> and
  192. # Peter Seibel <peter@weblogic.com>
  193. package Error::subs;
  194. use Exporter ();
  195. use vars qw(@EXPORT_OK @ISA %EXPORT_TAGS);
  196. @EXPORT_OK = qw(try with finally except otherwise);
  197. %EXPORT_TAGS = (try => \@EXPORT_OK);
  198. @ISA = qw(Exporter);
  199. sub run_clauses ($$$\@) {
  200. my($clauses,$err,$wantarray,$result) = @_;
  201. my $code = undef;
  202. $err = new Error::Simple($err) unless ref($err);
  203. CATCH: {
  204. # catch
  205. my $catch;
  206. if(defined($catch = $clauses->{'catch'})) {
  207. my $i = 0;
  208. CATCHLOOP:
  209. for( ; $i < @$catch ; $i += 2) {
  210. my $pkg = $catch->[$i];
  211. unless(defined $pkg) {
  212. #except
  213. splice(@$catch,$i,2,$catch->[$i+1]->());
  214. $i -= 2;
  215. next CATCHLOOP;
  216. }
  217. elsif($err->isa($pkg)) {
  218. $code = $catch->[$i+1];
  219. while(1) {
  220. my $more = 0;
  221. local($Error::THROWN);
  222. my $ok = eval {
  223. if($wantarray) {
  224. @{$result} = $code->($err,\$more);
  225. }
  226. elsif(defined($wantarray)) {
  227. @{$result} = ();
  228. $result->[0] = $code->($err,\$more);
  229. }
  230. else {
  231. $code->($err,\$more);
  232. }
  233. 1;
  234. };
  235. if( $ok ) {
  236. next CATCHLOOP if $more;
  237. undef $err;
  238. }
  239. else {
  240. $err = defined($Error::THROWN)
  241. ? $Error::THROWN : $@;
  242. $err = new Error::Simple($err)
  243. unless ref($err);
  244. }
  245. last CATCH;
  246. };
  247. }
  248. }
  249. }
  250. # otherwise
  251. my $owise;
  252. if(defined($owise = $clauses->{'otherwise'})) {
  253. my $code = $clauses->{'otherwise'};
  254. my $more = 0;
  255. my $ok = eval {
  256. if($wantarray) {
  257. @{$result} = $code->($err,\$more);
  258. }
  259. elsif(defined($wantarray)) {
  260. @{$result} = ();
  261. $result->[0] = $code->($err,\$more);
  262. }
  263. else {
  264. $code->($err,\$more);
  265. }
  266. 1;
  267. };
  268. if( $ok ) {
  269. undef $err;
  270. }
  271. else {
  272. $err = defined($Error::THROWN)
  273. ? $Error::THROWN : $@;
  274. $err = new Error::Simple($err)
  275. unless ref($err);
  276. }
  277. }
  278. }
  279. $err;
  280. }
  281. sub try (&;$) {
  282. my $try = shift;
  283. my $clauses = @_ ? shift : {};
  284. my $ok = 0;
  285. my $err = undef;
  286. my @result = ();
  287. unshift @Error::STACK, $clauses;
  288. my $wantarray = wantarray();
  289. do {
  290. local $Error::THROWN = undef;
  291. $ok = eval {
  292. if($wantarray) {
  293. @result = $try->();
  294. }
  295. elsif(defined $wantarray) {
  296. $result[0] = $try->();
  297. }
  298. else {
  299. $try->();
  300. }
  301. 1;
  302. };
  303. $err = defined($Error::THROWN) ? $Error::THROWN : $@
  304. unless $ok;
  305. };
  306. shift @Error::STACK;
  307. $err = run_clauses($clauses,$err,wantarray,@result)
  308. unless($ok);
  309. $clauses->{'finally'}->()
  310. if(defined($clauses->{'finally'}));
  311. throw $err if defined($err);
  312. wantarray ? @result : $result[0];
  313. }
  314. # Each clause adds a sub to the list of clauses. The finally clause is
  315. # always the last, and the otherwise clause is always added just before
  316. # the finally clause.
  317. #
  318. # All clauses, except the finally clause, add a sub which takes one argument
  319. # this argument will be the error being thrown. The sub will return a code ref
  320. # if that clause can handle that error, otherwise undef is returned.
  321. #
  322. # The otherwise clause adds a sub which unconditionally returns the users
  323. # code reference, this is why it is forced to be last.
  324. #
  325. # The catch clause is defined in Error.pm, as the syntax causes it to
  326. # be called as a method
  327. sub with (&;$) {
  328. @_
  329. }
  330. sub finally (&) {
  331. my $code = shift;
  332. my $clauses = { 'finally' => $code };
  333. $clauses;
  334. }
  335. # The except clause is a block which returns a hashref or a list of
  336. # key-value pairs, where the keys are the classes and the values are subs.
  337. sub except (&;$) {
  338. my $code = shift;
  339. my $clauses = shift || {};
  340. my $catch = $clauses->{'catch'} ||= [];
  341. my $sub = sub {
  342. my $ref;
  343. my(@array) = $code->($_[0]);
  344. if(@array == 1 && ref($array[0])) {
  345. $ref = $array[0];
  346. $ref = [ %$ref ]
  347. if(UNIVERSAL::isa($ref,'HASH'));
  348. }
  349. else {
  350. $ref = \@array;
  351. }
  352. @$ref
  353. };
  354. unshift @{$catch}, undef, $sub;
  355. $clauses;
  356. }
  357. sub otherwise (&;$) {
  358. my $code = shift;
  359. my $clauses = shift || {};
  360. if(exists $clauses->{'otherwise'}) {
  361. require Carp;
  362. Carp::croak("Multiple otherwise clauses");
  363. }
  364. $clauses->{'otherwise'} = $code;
  365. $clauses;
  366. }
  367. 1;
  368. __END__
  369. =head1 NAME
  370. Error - Error/exception handling in an OO-ish way
  371. =head1 SYNOPSIS
  372. use Error qw(:try);
  373. throw Error::Simple( "A simple error");
  374. sub xyz {
  375. ...
  376. record Error::Simple("A simple error")
  377. and return;
  378. }
  379. unlink($file) or throw Error::Simple("$file: $!",$!);
  380. try {
  381. do_some_stuff();
  382. die "error!" if $condition;
  383. throw Error::Simple -text => "Oops!" if $other_condition;
  384. }
  385. catch Error::IO with {
  386. my $E = shift;
  387. print STDERR "File ", $E->{'-file'}, " had a problem\n";
  388. }
  389. except {
  390. my $E = shift;
  391. my $general_handler=sub {send_message $E->{-description}};
  392. return {
  393. UserException1 => $general_handler,
  394. UserException2 => $general_handler
  395. };
  396. }
  397. otherwise {
  398. print STDERR "Well I don't know what to say\n";
  399. }
  400. finally {
  401. close_the_garage_door_already(); # Should be reliable
  402. }; # Don't forget the trailing ; or you might be surprised
  403. =head1 DESCRIPTION
  404. The C<Error> package provides two interfaces. Firstly C<Error> provides
  405. a procedural interface to exception handling. Secondly C<Error> is a
  406. base class for errors/exceptions that can either be thrown, for
  407. subsequent catch, or can simply be recorded.
  408. Errors in the class C<Error> should not be thrown directly, but the
  409. user should throw errors from a sub-class of C<Error>.
  410. =head1 PROCEDURAL INTERFACE
  411. C<Error> exports subroutines to perform exception handling. These will
  412. be exported if the C<:try> tag is used in the C<use> line.
  413. =over 4
  414. =item try BLOCK CLAUSES
  415. C<try> is the main subroutine called by the user. All other subroutines
  416. exported are clauses to the try subroutine.
  417. The BLOCK will be evaluated and, if no error is throw, try will return
  418. the result of the block.
  419. C<CLAUSES> are the subroutines below, which describe what to do in the
  420. event of an error being thrown within BLOCK.
  421. =item catch CLASS with BLOCK
  422. This clauses will cause all errors that satisfy C<$err-E<gt>isa(CLASS)>
  423. to be caught and handled by evaluating C<BLOCK>.
  424. C<BLOCK> will be passed two arguments. The first will be the error
  425. being thrown. The second is a reference to a scalar variable. If this
  426. variable is set by the catch block then, on return from the catch
  427. block, try will continue processing as if the catch block was never
  428. found.
  429. To propagate the error the catch block may call C<$err-E<gt>throw>
  430. If the scalar reference by the second argument is not set, and the
  431. error is not thrown. Then the current try block will return with the
  432. result from the catch block.
  433. =item except BLOCK
  434. When C<try> is looking for a handler, if an except clause is found
  435. C<BLOCK> is evaluated. The return value from this block should be a
  436. HASHREF or a list of key-value pairs, where the keys are class names
  437. and the values are CODE references for the handler of errors of that
  438. type.
  439. =item otherwise BLOCK
  440. Catch any error by executing the code in C<BLOCK>
  441. When evaluated C<BLOCK> will be passed one argument, which will be the
  442. error being processed.
  443. Only one otherwise block may be specified per try block
  444. =item finally BLOCK
  445. Execute the code in C<BLOCK> either after the code in the try block has
  446. successfully completed, or if the try block throws an error then
  447. C<BLOCK> will be executed after the handler has completed.
  448. If the handler throws an error then the error will be caught, the
  449. finally block will be executed and the error will be re-thrown.
  450. Only one finally block may be specified per try block
  451. =back
  452. =head1 CLASS INTERFACE
  453. =head2 CONSTRUCTORS
  454. The C<Error> object is implemented as a HASH. This HASH is initialized
  455. with the arguments that are passed to it's constructor. The elements
  456. that are used by, or are retrievable by the C<Error> class are listed
  457. below, other classes may add to these.
  458. -file
  459. -line
  460. -text
  461. -value
  462. -object
  463. If C<-file> or C<-line> are not specified in the constructor arguments
  464. then these will be initialized with the file name and line number where
  465. the constructor was called from.
  466. If the error is associated with an object then the object should be
  467. passed as the C<-object> argument. This will allow the C<Error> package
  468. to associate the error with the object.
  469. The C<Error> package remembers the last error created, and also the
  470. last error associated with a package. This could either be the last
  471. error created by a sub in that package, or the last error which passed
  472. an object blessed into that package as the C<-object> argument.
  473. =over 4
  474. =item throw ( [ ARGS ] )
  475. Create a new C<Error> object and throw an error, which will be caught
  476. by a surrounding C<try> block, if there is one. Otherwise it will cause
  477. the program to exit.
  478. C<throw> may also be called on an existing error to re-throw it.
  479. =item with ( [ ARGS ] )
  480. Create a new C<Error> object and returns it. This is defined for
  481. syntactic sugar, eg
  482. die with Some::Error ( ... );
  483. =item record ( [ ARGS ] )
  484. Create a new C<Error> object and returns it. This is defined for
  485. syntactic sugar, eg
  486. record Some::Error ( ... )
  487. and return;
  488. =back
  489. =head2 STATIC METHODS
  490. =over 4
  491. =item prior ( [ PACKAGE ] )
  492. Return the last error created, or the last error associated with
  493. C<PACKAGE>
  494. =back
  495. =head2 OBJECT METHODS
  496. =over 4
  497. =item stacktrace
  498. If the variable C<$Error::Debug> was non-zero when the error was
  499. created, then C<stacktrace> returns a string created by calling
  500. C<Carp::longmess>. If the variable was zero the C<stacktrace> returns
  501. the text of the error appended with the filename and line number of
  502. where the error was created, providing the text does not end with a
  503. newline.
  504. =item object
  505. The object this error was associated with
  506. =item file
  507. The file where the constructor of this error was called from
  508. =item line
  509. The line where the constructor of this error was called from
  510. =item text
  511. The text of the error
  512. =back
  513. =head2 OVERLOAD METHODS
  514. =over 4
  515. =item stringify
  516. A method that converts the object into a string. This method may simply
  517. return the same as the C<text> method, or it may append more
  518. information. For example the file name and line number.
  519. By default this method returns the C<-text> argument that was passed to
  520. the constructor, or the string C<"Died"> if none was given.
  521. =item value
  522. A method that will return a value that can be associated with the
  523. error. For example if an error was created due to the failure of a
  524. system call, then this may return the numeric value of C<$!> at the
  525. time.
  526. By default this method returns the C<-value> argument that was passed
  527. to the constructor.
  528. =back
  529. =head1 PRE-DEFINED ERROR CLASSES
  530. =over 4
  531. =item Error::Simple
  532. This class can be used to hold simple error strings and values. It's
  533. constructor takes two arguments. The first is a text value, the second
  534. is a numeric value. These values are what will be returned by the
  535. overload methods.
  536. If the text value ends with C<at file line 1> as $@ strings do, then
  537. this infomation will be used to set the C<-file> and C<-line> arguments
  538. of the error object.
  539. This class is used internally if an eval'd block die's with an error
  540. that is a plain string.
  541. =back
  542. =head1 KNOWN BUGS
  543. None, but that does not mean there are not any.
  544. =head1 AUTHORS
  545. Graham Barr, gbarr@pobox.com
  546. The code that inspired me to write this was originally written by
  547. Peter Seibel E<lt>peter@weblogic.comE<gt> and adapted by Jesse Glick
  548. E<lt>jglick@sig.bsh.comE<gt>.
  549. =head1 MAINTAINER
  550. Arun Kumar U, u_arunkumar@yahoo.com
  551. =cut