| 1 | # $Header: /home/mike/cvs/mike/zSQLgate/lib/Net/Z3950/DBIServer/Exception.pm,v 1.3 2002-03-24 23:29:34 mike Exp $ | ||
| 2 | |||
| 3 | package Net::Z3950::DBIServer::Exception; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Net::Z3950::DBIServer::Exception - exception thrown by DBIServer module | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | # Somewhere in a function deep in the call tree | ||
| 13 | die new Net::Z3950::DBIServer::Exception($bib1ErrorCode, $addInfo); | ||
| 14 | |||
| 15 | # And higher up the call tree we invoke and check like this: | ||
| 16 | eval { | ||
| 17 | functionWhichMightThrowAnException(); | ||
| 18 | }; if ($@ && $@->isa('Net::Z3950::DBIServer::Exception')) { | ||
| 19 | warn "Bib-1 error: " . $@->code() . ", " . $@->addinfo(); | ||
| 20 | } elsif ($@) { | ||
| 21 | die $@; | ||
| 22 | } | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | A tiny class which merely encapsulates a BIB1 diagnostic so that it | ||
| 27 | can be generated by low-level functions and picked up at a higher | ||
| 28 | level. Exists only to be thrown (oh, alright then, C<die()>d :-) | ||
| 29 | |||
| 30 | Note that code which catches exceptions thrown in this way needs to be | ||
| 31 | careful that it only catches the right kinds of objects, as the code | ||
| 32 | above does. This is essential as Perl itself will call C<die()> - | ||
| 33 | with a string argument - when certain ``real errors'' occur, such as | ||
| 34 | trying to call a function which has not been defined. | ||
| 35 | |||
| 36 | =head1 METHODS | ||
| 37 | |||
| 38 | =head2 new() | ||
| 39 | |||
| 40 | new Net::Z3950::DBIServer::Exception($bib1ErrorCode, $addInfo); | ||
| 41 | |||
| 42 | Creates and returns a new Exception object with the specified Bib-1 | ||
| 43 | error code and associated additional information. The only thing you | ||
| 44 | can do with the object thereafter is retrieve the error code and | ||
| 45 | additional information with which it was created. It is intended to | ||
| 46 | be used as the argument to C<die()>, which is Perl's, shall we say, | ||
| 47 | idiosyncratic exception-throwing primitive. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub new { | ||
| 52 | my $class = shift(); | ||
| 53 | my($code, $addinfo) = @_; | ||
| 54 | |||
| 55 | return bless { | ||
| 56 | code => $code, | ||
| 57 | addinfo => $addinfo || "", | ||
| 58 | }, $class; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | =head2 throw() | ||
| 63 | |||
| 64 | Net::Z3950::DBIServer::Exception::throw($bib1ErrorCode, $addInfo); | ||
| 65 | |||
| 66 | This convenience method is exactly equivalent to | ||
| 67 | |||
| 68 | die new Net::Z3950::DBIServer::Exception($bib1ErrorCode, $addInfo); | ||
| 69 | |||
| 70 | but one character shorter! | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub throw { | ||
| 75 | my($code, $addinfo) = @_; | ||
| 76 | |||
| 77 | die __PACKAGE__->new($code, $addinfo); | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | =head2 code() | ||
| 82 | |||
| 83 | $bib1ErrorCode = $exception->code(); | ||
| 84 | |||
| 85 | Returns the Bib-1 error code with which the exception was created. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub code { | ||
| 90 | my $this = shift(); | ||
| 91 | |||
| 92 | return $this->{code}; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | =head2 addinfo() | ||
| 97 | |||
| 98 | $addInfo = $exception->addinfo(); | ||
| 99 | |||
| 100 | Returns the additional information with which the exception was created. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub addinfo { | ||
| 105 | my $this = shift(); | ||
| 106 | |||
| 107 | return $this->{addinfo}; | ||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | =head1 AUTHOR | ||
| 112 | |||
| 113 | Mike Taylor E<lt>mike@miketaylor.org.ukE<gt> | ||
| 114 | |||
| 115 | First version Saturday 23rd February 2002. | ||
| 116 | |||
| 117 | =head1 SEE ALSO | ||
| 118 | |||
| 119 | C<Net::Z3950::DBIServer> | ||
| 120 | and | ||
| 121 | C<Net::Z3950::DBIServer::ResultSet> | ||
| 122 | are the modules that use this. | ||
| 123 | |||
| 124 | The numeric codes, their interpretations, and the form of additional | ||
| 125 | information that should accompany them are described in Appendix ERR.1 | ||
| 126 | of the Z39.50-1995 standard document at | ||
| 127 | http://lcweb.loc.gov/z3950/agency/defns/bib1diag.html | ||
| 128 | |||
| 129 | Isn't it great that the standard is freely available on the web? Hang | ||
| 130 | your heads in shame, ISO! | ||
| 131 | |||
| 132 | =cut | ||
| 133 | |||
| 134 | |||
| 135 | 1; |