implemented onerror, onmethoderror and ontransporterror. moved the callbacks into...
authorerickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Mon, 24 Mar 2008 17:34:44 +0000 (17:34 +0000)
committererickson <erickson@9efc2488-bf62-4759-914b-345cdb29e865>
Mon, 24 Mar 2008 17:34:44 +0000 (17:34 +0000)
git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1290 9efc2488-bf62-4759-914b-345cdb29e865

src/javascript/opensrf.js
src/javascript/opensrf_xhr.js

index 3f5cb0f..ca86ba5 100644 (file)
@@ -174,9 +174,14 @@ OpenSRF.ClientSession.prototype.find_request = function(reqid) {
 OpenSRF.Request = function(session, reqid, args) {
     this.session = session;
     this.reqid = reqid;
+
+    /* callbacks */
     this.onresponse = args.onresponse;
-    this.onerror = args.onerror;
     this.oncomplete = args.oncomplete;
+    this.onerror = args.onerror;
+    this.onmethoderror = args.onmethoderror;
+    this.ontransporterror = args.ontransporterror;
+
     this.method = args.method;
     this.params = args.params;
     this.timeout = args.timeout;
@@ -203,7 +208,9 @@ OpenSRF.Request.prototype.send = function() {
         'timeout' : this.timeout,
         'onresponse' : this.onresponse,
         'oncomplete' : this.oncomplete,
-        'onerror' : this.onerror
+        'onerror' : this.onerror,
+        'onmethoderror' : this.onmethoderror,
+        'ontransporterror' : this.ontransporterror
     });
 }
 
@@ -217,16 +224,16 @@ OpenSRF.NetMessage = function(to, from, thread, body) {
 OpenSRF.Stack = function() {
 }
 
-OpenSRF.Stack.push = function(net_msg, stack_callback) {
+OpenSRF.Stack.push = function(net_msg, callbacks) {
     var ses = OpenSRF.Session.find_session(net_msg.thread); 
     if(!ses) return;
     ses.remote_id = net_msg.sender;
     osrf_msgs = JSON2js(net_msg.body);
     for(var i = 0; i < osrf_msgs.length; i++) 
-        OpenSRF.Stack.handle_message(ses, osrf_msgs[i], stack_callback);        
+        OpenSRF.Stack.handle_message(ses, osrf_msgs[i], callbacks);        
 }
 
-OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
+OpenSRF.Stack.handle_message = function(ses, osrf_msg, callbacks) {
     
     var req = null;
 
@@ -238,27 +245,40 @@ OpenSRF.Stack.handle_message = function(ses, osrf_msg, stack_callback) {
 
         if(status == OSRF_STATUS_COMPLETE) {
             req = ses.find_request(osrf_msg.threadTrace());
-            if(req) req.complete = true;
+            if(req) {
+                req.complete = true;
+                if(callbacks.oncomplete && !req.oncomplete_called) {
+                    req.oncomplete_called = true;
+                    return callbacks.oncomplete(req);
+                }
+            }
         }
 
         if(status == OSRF_STATUS_OK) {
             ses.state = OSRF_APP_SESSION_CONNECTED;
+
+            /* call the connect callback */
+            if(ses.onconnect && !ses.onconnect_called) {
+                ses.onconnect_called = true;
+                return ses.onconnect();
+            }
         }
 
         if(status == OSRF_STATUS_NOTFOUND) {
-            alert('NOT_FOUND: ' + status_text);
-            return;
+            req = ses.find_request(osrf_msg.threadTrace());
+            if(callbacks.onmethoderror) 
+                return callbacks.onmethoderror(req, status, status_text);
         }
     }
 
     if(osrf_msg.type() == OSRF_MESSAGE_TYPE_RESULT) {
         req = ses.find_request(osrf_msg.threadTrace());
-        if(req) 
+        if(req) {
             req.response_queue.push(osrf_msg.payload());
+            if(callbacks.onresponse) 
+                return callbacks.onresponse(req);
+        }
     }
-
-    if(stack_callback)
-        stack_callback(ses, req);
 }
 
 /* The following classes map directly to network-serializable opensrf objects */
index d10f885..81d7e3d 100644 (file)
@@ -75,36 +75,42 @@ OpenSRF.XHRequest.prototype.send = function() {
     return this;
 }
 
-
 OpenSRF.XHRequest.prototype.core_handler = function() {
     sender = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_FROM);
     thread = this.xreq.getResponseHeader(OSRF_HTTP_HEADER_THREAD);
     json = this.xreq.responseText;
     stat = this.xreq.status;
 
-    var xhr = this;
+    if(stat >= 400) 
+        return this.transport_error_handler();
+
     OpenSRF.Stack.push(
         new OpenSRF.NetMessage(null, sender, thread, json),
+        {
+            onresponse : this.args.onresponse,
+            oncomplete : this.args.oncomplete,
+            onerror : this.args.onerror,
+            onmethoderror : this.method_error_handler()
+        }
+    );
+}
 
-        function(ses, req) {
-            if(ses) {
-                if(ses.state == OSRF_APP_SESSION_CONNECTED && 
-                    ses.onconnect && !ses.onconnect_called) {
-                        ses.onconnect_called = true;
-                        ses.onconnect();
-                }
-            }
 
-            if(req) {
-                if(req.response_queue.length > 0 && xhr.args.onresponse) 
-                    return xhr.args.onresponse(req);
+OpenSRF.XHRequest.prototype.method_error_handler = function() {
+    var xhr = this;
+    return function(req, status, status_text) {
+        if(xhr.args.onmethoderror) 
+            xhr.args.onmethoderror(req, status, status_text);
+        if(xhr.args.onerror)  
+            xhr.args.onerror(xhr.message, xhr.args.rcpt || xhr.args.rcpt_service, xhr.args.thread);
+    }
+}
 
-                if(req.complete && xhr.args.oncomplete && !xhr.args.oncomplete_called) {
-                    xhr.args.oncomplete_called = true;
-                    return xhr.args.oncomplete(req);
-                }
-            }
-        }
-    );
+OpenSRF.XHRequest.prototype.transport_error_handler = function() {
+    if(this.args.ontransporterror) 
+        this.args.ontransporterror(this.xreq);
+    if(this.args.onerror) 
+        this.args.onerror(this.message, this.args.rcpt || this.args.rcpt_service, this.args.thread);
 }
 
+