PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/perl5/ORAC/Error.pm

https://github.com/ocoskun/ORAC-DR
Perl | 292 lines | 219 code | 62 blank | 11 comment | 3 complexity | 8ca6f1b2bb3d848c9955b8a31b46a051 MD5 | raw file
  1. # ORAC::Error --------------------------------------------------------------
  2. package ORAC::Error;
  3. =head1 NAME
  4. ORAC::Error - Exception handling in an object orientated manner.
  5. =head1 SYNOPSIS
  6. use ORAC::Error qw /:try/;
  7. use ORAC::Constants qw /:status/;
  8. # throw an error to be caught
  9. throw ORAC::Error::UserAbort( $message, ORAC__ABORT );
  10. throw ORAC::Error::FatalError( $message, ORAC__FATAL );
  11. # record and then retrieve an error
  12. do_stuff();
  13. my $Error = ORAC::Error->prior;
  14. ORAC::Error->flush if defined $Error;
  15. sub do_stuff {
  16. record ORAC::Error::FatalError( $message, ORAC__FATAL);
  17. }
  18. # try and catch blocks
  19. try {
  20. stuff();
  21. }
  22. catch ORAC::Error::UserAbort with
  23. {
  24. # normally we just want to catch and then ignore UserAborts
  25. my $Error = shift;
  26. orac_exit_normally();
  27. }
  28. catch ORAC::Error::FatalError with
  29. {
  30. # its a fatal error
  31. my $Error = shift;
  32. orac_exit_normally($Error);
  33. }
  34. otherwise
  35. {
  36. # this block catches croaks and other dies
  37. my $Error = shift;
  38. orac_exit_normally($Error);
  39. }; # Don't forget the trailing semi-colon to close the catch block
  40. =head1 DESCRIPTION
  41. C<ORAC::Error> is based on a modifed version of Graham Barr's C<Error>
  42. package, and more documentation about the (many) features present in
  43. the module but currently unused by ORAC-DR can be found in the
  44. documentation for that module.
  45. As with the C<Error> package, C<ORAC::Error> provides two interfaces.
  46. Firstly it provides a procedural interface to exception handling, and
  47. secondly C<ORAC::Error> is a base class for exceptions that can either
  48. be thrown, for subsequent catch, or can simply be recorded.
  49. If you wish to throw an C<FatalError> or C<UserAbort> then you should
  50. also C<use ORAC::Constants qw / :status /> so that the ORAC constants
  51. are available.
  52. =head1 PROCEDURAL INTERFACE
  53. C<ORAC::Error> exports subroutines to perform exception
  54. handling. These will be exported if the C<:try> tag is used in the
  55. C<use> line.
  56. =over 4
  57. =item try BLOCK CLAUSES
  58. C<try> is the main subroutine called by the user. All other
  59. subroutines exported are clauses to the try subroutine.
  60. The BLOCK will be evaluated and, if no error is throw, try will return
  61. the result of the block.
  62. C<CLAUSES> are the subroutines below, which describe what to do in the
  63. event of an error being thrown within BLOCK.
  64. =item catch CLASS with BLOCK
  65. This clauses will cause all errors that satisfy
  66. C<$err-E<gt>isa(CLASS)> to be caught and handled by evaluating
  67. C<BLOCK>.
  68. C<BLOCK> will be passed two arguments. The first will be the error
  69. being thrown. The second is a reference to a scalar variable. If this
  70. variable is set by the catch block then, on return from the catch
  71. block, try will continue processing as if the catch block was never
  72. found.
  73. To propagate the error the catch block may call C<$err-E<gt>throw>
  74. If the scalar reference by the second argument is not set, and the
  75. error is not thrown. Then the current try block will return with the
  76. result from the catch block.
  77. =item otherwise BLOCK
  78. Catch I<any> error by executing the code in C<BLOCK>
  79. When evaluated C<BLOCK> will be passed one argument, which will be the
  80. error being processed.
  81. Only one otherwise block may be specified per try block
  82. =back
  83. =head1 CLASS INTERFACE
  84. =head2 CONSTRUCTORS
  85. The C<ORAC::Error> object is implemented as a HASH. This HASH is
  86. initialized with the arguments that are passed to it's
  87. constructor. The elements that are used by, or are retrievable by the
  88. C<ORAC::Error> class are listed below, other classes may add to these.
  89. -file
  90. -line
  91. -text
  92. -value
  93. If C<-file> or C<-line> are not specified in the constructor arguments
  94. then these will be initialized with the file name and line number
  95. where the constructor was called from.
  96. The C<ORAC::Error> package remembers the last error created, and also
  97. the last error associated with a package.
  98. =over 4
  99. =item throw ( [ ARGS ] )
  100. Create a new C<ORAC::Error> object and throw an error, which will be
  101. caught by a surrounding C<try> block, if there is one. Otherwise it
  102. will cause the program to exit.
  103. C<throw> may also be called on an existing error to re-throw it.
  104. =item with ( [ ARGS ] )
  105. Create a new C<ORAC::Error> object and returns it. This is defined for
  106. syntactic sugar, eg
  107. die with ORAC::Error::FatalError ( $message, ORAC__FATAL );
  108. =item record ( [ ARGS ] )
  109. Create a new C<ORAC::Error> object and returns it. This is defined for
  110. syntactic sugar, eg
  111. record ORAC::Error::UserAbort ( $message, ORAC__ABORT )
  112. and return;
  113. =back
  114. =head2 METHODS
  115. =over 4
  116. =item prior ( [ PACKAGE ] )
  117. Return the last error created, or the last error associated with
  118. C<PACKAGE>
  119. my $Error = ORAC::Error->prior;
  120. =item flush ( [ PACKAGE ] )
  121. Flush the last error created, or the last error associated with
  122. C<PACKAGE>.It is necessary to clear the error stack before exiting the
  123. package or uncaught errors generated using C<record> will be reported.
  124. $Error->flush;
  125. =back
  126. =head2 OVERLOAD METHODS
  127. =over 4
  128. =item stringify
  129. A method that converts the object into a string. By default it returns
  130. the C<-text> argument that was passed to the constructor, appending
  131. the line and file where the exception was generated.
  132. =item value
  133. A method that will return a value that can be associated with the
  134. error. By default this method returns the C<-value> argument that was
  135. passed to the constructor.
  136. =back
  137. =head1 PRE-DEFINED ERROR CLASSES
  138. =over 4
  139. =item ORAC::Error::FatalError
  140. Used for fatal errors where we want the pipeline to die with cause.
  141. This class can be used to hold simple error strings and values. It's
  142. constructor takes two arguments. The first is a text value, the second
  143. is a numeric value, C<ORAC__FATAL>. These values are what will be
  144. returned by the overload methods.
  145. =item ORAC::Error::UserAbort
  146. Used for user generated pipeline aborts, which are handled slightly
  147. differently than fatal errors generated by the pipeline itself. The
  148. constructor for a C<UserAbort> is similar to that for a C<FatalError>
  149. except that the numeric value C<ORAC__ABORT> is passed.
  150. =item ORAC::Error::TermProcessing
  151. Terminate processing of this recipe but continue on as if everything
  152. is fine. This can be used to stop a recipe early but will not
  153. trigger any long term errors to propagate through the pipeline.
  154. =item ORAC::Error::TermProcessingErr
  155. As for TermProcessing except that whilst the pipeline will continue
  156. to run the exit status of the pipeline will be non-zero to indicate
  157. that a problem was found. This can be used when the pipeline can
  158. work around the problem but would like the final exit status to
  159. reflect that there is a problem to be investigated.
  160. =back
  161. =head1 KNOWN PROBLEMS
  162. C<ORAC::Error> which are thrown and not caught inside a C<try> block
  163. will in turn be caught by C<Tk::Error> if used inside a Tk
  164. environment, as will C<croak> and C<die>. However if is a C<croak> or
  165. C<die> is generated inside a try block and no C<otherwise> block
  166. exists to catch the exception it will be silently ignored until the
  167. application exits, when it will be reported.
  168. =head1 AUTHORS
  169. Tim Jenness E<lt>t.jenness@jach.hawaii.eduE<gt>,
  170. Alasdair Allan E<lt>aa@astro.ex.ac.ukE<gt>
  171. =head1 ACKNOWLEDGMENTS
  172. This class is a slightly modified, with the addition of the C<flush>
  173. method, version of Graham Barr's (gbarr@pobox.com) C<Error>
  174. class. That code was in turn based on code written by Peter Seibel
  175. (peter@weblogic.com) and Jesse Glick (jglick@sig.bsh.com).
  176. =cut
  177. use Error qw/ :try /;
  178. use warnings;
  179. use strict;
  180. use vars qw/$VERSION/;
  181. $VERSION = '1.0';
  182. # flush method added to the base class
  183. use base qw/ Error::Simple /;
  184. # ORAC::Error::UserAbort ---------------------------------------------------
  185. package ORAC::Error::UserAbort;
  186. use base qw/ ORAC::Error /;
  187. # ORAC::Error::FatalError --------------------------------------------------
  188. package ORAC::Error::FatalError;
  189. use base qw/ ORAC::Error /;
  190. package ORAC::Error::TermProcessing;
  191. use base qw/ ORAC::Error /;
  192. package ORAC::Error::TermProcessingErr;
  193. use base qw/ ORAC::Error::TermProcessing /;
  194. # --------------------------------------------------------------------------
  195. 1;