X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=lib%2FZOOM.pm;h=b85163f64acda4a7b49ec28aa3bfa926ab7e1108;hb=6a4ccd8becf9b74935a3da6bc919225858a7d32a;hp=a88be643ba026044927c98987ac6f7821d560a9a;hpb=9140a0be284807f6509e1b3800c6093633940e17;p=ZOOM-Perl-moved-to-github.git diff --git a/lib/ZOOM.pm b/lib/ZOOM.pm index a88be64..b85163f 100644 --- a/lib/ZOOM.pm +++ b/lib/ZOOM.pm @@ -1,5 +1,3 @@ -# $Id: ZOOM.pm,v 1.42 2006-11-28 16:47:19 mike Exp $ - use strict; use warnings; use IO::File; @@ -34,7 +32,7 @@ sub MEMORY { Net::Z3950::ZOOM::ERROR_MEMORY } sub ENCODE { Net::Z3950::ZOOM::ERROR_ENCODE } sub DECODE { Net::Z3950::ZOOM::ERROR_DECODE } sub CONNECTION_LOST { Net::Z3950::ZOOM::ERROR_CONNECTION_LOST } -sub INIT { Net::Z3950::ZOOM::ERROR_INIT } +sub ZINIT { Net::Z3950::ZOOM::ERROR_INIT } sub INTERNAL { Net::Z3950::ZOOM::ERROR_INTERNAL } sub TIMEOUT { Net::Z3950::ZOOM::ERROR_TIMEOUT } sub UNSUPPORTED_PROTOCOL { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_PROTOCOL } @@ -113,6 +111,12 @@ sub diag_str { return Net::Z3950::ZOOM::diag_str($code); } +sub diag_srw_str { + my($code) = @_; + + return Net::Z3950::ZOOM::diag_srw_str($code); +} + sub event_str { return Net::Z3950::ZOOM::event_str(@_); } @@ -141,8 +145,11 @@ sub new { $diagset ||= "ZOOM"; if (uc($diagset) eq "ZOOM" || uc($diagset) eq "BIB-1") { $message ||= ZOOM::diag_str($code); + } elsif (lc($diagset) eq "info:srw/diagnostic/1") { + $message ||= ZOOM::diag_srw_str($code); } else { - # Should fill in messages for other diagsets, too. + # Should fill in messages for any other known diagsets. + $message ||= "(unknown error)"; } return bless { @@ -175,7 +182,9 @@ sub diagset { sub render { my $this = shift(); - my $res = "ZOOM error " . $this->code() . ' "' . $this->message() . '"'; + + my $res = "ZOOM error " . $this->code(); + $res .= ' "' . $this->message() . '"' if $this->message(); $res .= ' (addinfo: "' . $this->addinfo() . '")' if $this->addinfo(); $res .= " from diag-set '" . $this->diagset() . "'" if $this->diagset(); return $res; @@ -337,12 +346,32 @@ sub _conn { sub _check { my $this = shift(); + my($always_die_on_error) = @_; my($errcode, $errmsg, $addinfo, $diagset) = (undef, "x", "x", "x"); $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg, $addinfo, $diagset); - die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset) - if $errcode; + if ($errcode) { + my $exception = new ZOOM::Exception($errcode, $errmsg, $addinfo, + $diagset); + if (!$this->option("async") || $always_die_on_error) { + ZOOM::Log::log("zoom_check", "throwing error $exception"); + die $exception; + } else { + ZOOM::Log::log("zoom_check", "not reporting error $exception"); + } + } +} + +# This wrapper for _check() is called only from outside the ZOOM +# module, and therefore only in situations where an asynchronous +# application is actively asking for an exception to be thrown if an +# error has been detected. So it passed always_die_on_error=1 to the +# underlying _check() method. +# +sub check { + my $this = shift(); + return $this->_check(1); } sub create { @@ -382,6 +411,14 @@ sub error_x { return wantarray() ? ($errcode, $errmsg, $addinfo, $diagset) : $errcode; } +sub exception { + my $this = shift(); + + my($errcode, $errmsg, $addinfo, $diagset) = $this->error_x(); + return undef if $errcode == 0; + return new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset); +} + sub errcode { my $this = shift(); return Net::Z3950::ZOOM::connection_errcode($this->_conn()); @@ -499,6 +536,12 @@ sub is_idle { return Net::Z3950::ZOOM::connection_is_idle($this->_conn()); } +sub peek_event { + my $this = shift(); + + return Net::Z3950::ZOOM::connection_peek_event($this->_conn()); +} + sub destroy { my $this = shift(); @@ -831,6 +874,7 @@ sub exception { my $this = shift(); my($errcode, $errmsg, $addinfo, $diagset) = $this->error(); + return undef if $errcode == 0; return new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset); } @@ -852,14 +896,7 @@ sub get { my($type, $args) = @_; $type = "$type;$args" if defined $args; - my $len = 0; - my $string = Net::Z3950::ZOOM::record_get($this->_rec(), $type, $len); - # I don't think we need '$len' at all. ### Probably the Perl-to-C - # glue code should use the value of `len' as well as the opaque - # data-pointer returned, to ensure that the SV contains all of the - # returned data and does not stop at the first NUL character in - # binary data. Carefully check the ZOOM_record_get() documentation. - return $string; + return Net::Z3950::ZOOM::record_get($this->_rec(), $type); } sub clone { @@ -1067,5 +1104,6 @@ sub log { Net::Z3950::ZOOM::yaz_log($level, join("", @message)); } +BEGIN { ZOOM::Log::mask_str("zoom_check"); } 1;