New ->exception() method£
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
index e99ddc7..0a42e3e 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: ZOOM.pm,v 1.45 2007-02-13 15:31:26 mike Exp $
+# $Id: ZOOM.pm,v 1.48 2007-05-09 12:03:52 mike Exp $
 
 use strict;
 use warnings;
@@ -337,20 +337,34 @@ 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);
     if ($errcode) {
-       if ($this->option("_check_debug")) {
-           print STDERR "ZOOM WARNING!  $this->check() failed with error $diagset:$errcode ($errmsg) $addinfo\n";
-           print STDERR "SIG{SEGV} ", (defined $SIG{SEGV} ? ("= '" . $SIG{SEGV} . "'") : "undefined"), "'\n";
-           print STDERR "SIG{__DIE__} ", (defined $SIG{__DIE__} ? ("= '" . $SIG{__DIE__} , "'") : "undefined"), "'\n";
+       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");
        }
-       die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset);
     }
 }
 
+# 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 {
     my $class = shift();
     my(@options) = @_;
@@ -388,6 +402,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());
@@ -1080,5 +1102,6 @@ sub log {
     Net::Z3950::ZOOM::yaz_log($level, join("", @message));
 }
 
+BEGIN { ZOOM::Log::mask_str("zoom_check"); }
 
 1;