Better error handling.
[egate.git] / www / z39util.tcl
index 4322d2c..476673b 100644 (file)
@@ -1,5 +1,5 @@
 #
-# $Id: z39util.tcl,v 1.7 1995/11/10 14:47:32 adam Exp $
+# $Id: z39util.tcl,v 1.9 1995/11/13 18:17:48 adam Exp $
 #
 proc saveState {} {
     uplevel #0 {
@@ -35,7 +35,8 @@ proc search-response {sno} {
         set code [lindex $status 1]
         set msg [lindex $status 2]
         set addinfo [lindex $status 3]
-        html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
+        displayError "Diagnostic message" \
+                "$msg: $addinfo<br>\n(error code $code)"
         set sessionWait -2
     } else {
         set sessionWait 1
@@ -377,7 +378,7 @@ proc build-query {t} {
     return $q
 }
 
-proc z39search {setNo piggy tno} {
+proc z39search {setNo piggy tno elements} {
     global hist
     global sessionWait
 
@@ -405,38 +406,37 @@ proc z39search {setNo piggy tno} {
     if {$oldHost != $host} {
         catch [list $zz disconnect]
 
-        html "Connecting to target " $host " <br>\n"
         set sessionWait 0
         if {[catch [list $zz connect $host]]} {
-            html "Cannot connect to target ${host} <br>\n"
+            displayError "Cannot connect to target" $host
             return 0
         } elseif {$sessionWait == 0} {
             zwait sessionWait
             if {$sessionWait != 1} {
-                html "Cannot connect to target ${host} <br>\n"
+                displayError "Cannot connect to target" $host
                 return 0
             }
         }
         $zz idAuthentication $idAuth
         set sessionWait 0
-        if {[catch [list $zz init]]} {
-            html "Cannot initialize with target ${host} <br>\n"
+        if {[catch {$zz init}]} {
+            displayError "Cannot initialize target" $host
             return 0
         }
         if {[catch {zwait sessionWait 60}]} {
-            html "Cannot initialize with target ${host} <br>\n"
+            displayError "Cannot initialize target" $host
            $zz disconnect
             return 0
         }
         if {$sessionWait != "1"} {
-            html "Cannot initialize with target ${host} <br>\n"
+            displayError "Cannot initialize target" $host
            $zz disconnect
             return 0
         }
         if {![$zz initResult]} {
             set u [$zz userInformationField]
             $zz disconnect
-            html "Connection rejected by target: $u <br>\n"
+            displayError "Cannot initialize target $host" $u
             return 0
         }
     }
@@ -444,6 +444,9 @@ proc z39search {setNo piggy tno} {
         return 1
     }
     ir-set $zz.$setNo $zz
+    $zz.$setNo smallSetElementSetNames $elements
+    $zz.$setNo mediumSetElementSetNames $elements
+    $zz.$setNo recordElements $elements
     eval $zz.$setNo databaseNames $database
 
     $zz.$setNo preferredRecordSyntax USMARC
@@ -462,12 +465,14 @@ proc z39search {setNo piggy tno} {
     $zz.$setNo search $query
 
     if {[catch {zwait sessionWait 600}]} {
+        displayError "Timeout in search" {}
         html "</body></html>\n"
         $zz disconnect
         return 0
     }
         
     if {$sessionWait != 1} {
+        displayError "Search fail" "Connection closed"
         html "</body></html>\n"
         $zz disconnect
         return 0
@@ -477,7 +482,8 @@ proc z39search {setNo piggy tno} {
         set code [lindex $status 1]
         set msg [lindex $status 2]
         set addinfo [lindex $status 3]
-        html "<h2>Error NSD$code: $msg: $addinfo </h2><br>\n"
+        displayError "Diagnostic message" \
+                "$msg: $addinfo\n<br>(error code $code)"
         return 0
     }
     set hist($setNo,hits) [$zz.$setNo resultCount]
@@ -523,7 +529,7 @@ proc search-m-response {setNo i} {
     set zstatus($i) 2
 }
 
-proc z39msearch {setNo piggy} {
+proc z39msearch {setNo piggy elements} {
     global zleft
     global zstatus
     global hist
@@ -552,6 +558,7 @@ proc z39msearch {setNo piggy} {
             set zstatus($i) 1
             continue
         }
+        z39$i idAuthentication $hist($setNo,$i,idAuthentication)
         html "Connecting to target " $host " <br>\n"
         set zstatus($i) -1
         if {![catch {z39$i connect $host}]} {
@@ -572,6 +579,11 @@ proc z39msearch {setNo piggy} {
             ir-set z39$i.$setNo z39$i
             set hist($setNo,$i,offset) 0
             eval z39$i.$setNo databaseNames $hist($setNo,$i,database)
+
+            z39$i.$setNo smallSetElementSetNames $elements
+            z39$i.$setNo mediumSetElementSetNames $elements
+            z39$i.$setNo recordElements $elements
+
             z39$i.$setNo preferredRecordSyntax USMARC
             z39$i callback [list search-m-response $setNo $i]
 
@@ -607,7 +619,7 @@ proc z39msearch {setNo piggy} {
     }
 }
 
-proc z39present {setNo tno setOffset setMax dfunc} {
+proc z39present {setNo tno setOffset setMax dfunc elements} {
     global hist
     global sessionWait
 
@@ -617,6 +629,8 @@ proc z39present {setNo tno setOffset setMax dfunc} {
         set zz z39
     }
 
+    $zz.$setNo elementSetNames $elements
+    $zz.$setNo recordElements $elements
     set toGet [expr 1 + $setMax - $setOffset]
     while {$setMax > 0 && $toGet > 0} {
         for {set got 0} {$got < $toGet} {incr got} {
@@ -631,6 +645,9 @@ proc z39present {setNo tno setOffset setMax dfunc} {
                $zz disconnect
                 break
            }
+            if {$sessionWait == "0"} {
+                $zz disconnect
+            }
             if {$sessionWait != "1"} {
                 break
             }
@@ -660,7 +677,7 @@ proc z39history {} {
     for {set setNo 1} {$setNo < $nextSetNo} {incr setNo} {
         html {<dt> <a href="http:} $env(SCRIPT_NAME)
         html / $sessionId {/search.egw/} $setNo + 1
-        html + [expr $hist($setNo,maxPresent) - 1]
+        html + $hist($setNo,maxPresent)
         html {"> } [lindex $targets($hist($setNo,host)) 0]
         if {[llength $hist($setNo,database)] > 1} {
             html ": "
@@ -679,3 +696,13 @@ proc z39history {} {
     }
     html "</dl>\n"
 }
+
+proc displayError {msga msgb} {
+    html "<p><center>\n"
+    html {<img src="/gif/noway.gif">}
+    html "<h2>" $msga "</h2>\n"
+    if {$msgb != ""} {
+        html "<h3>" $msgb "</h3>\n"
+    }
+    html "</center><p>\n"
+}