Bladeren bron

shared error handlers.

Nicolas Cannasse 19 jaren geleden
bovenliggende
commit
b7fd6ece2a

+ 14 - 7
std/haxe/remoting/AsyncConnection.hx

@@ -28,22 +28,29 @@ class AsyncConnection implements Dynamic<AsyncConnection> {
 
 	var __data : Dynamic;
 	var __path : Array<String>;
+	var __error : { ref : Dynamic -> Void };
+	public var onError(getErrorHandler,setErrorHandler) : Dynamic -> Void;
 
 	function new( data : Dynamic, path ) {
 		__data = data;
 		__path = path;
+		__error = { ref : function(e) { } };
 	}
 
 	function __resolve(field) {
 		var s = new AsyncConnection(__data,__path.copy());
-		// late binding
-		var me = this;
-		s.onError = function(e) { me.onError(e); };
+		s.__error = __error;
 		s.__path.push(field);
 		return s;
 	}
 
-	public function onError( err : Dynamic ) {
+	function getErrorHandler() {
+		return __error.ref;
+	}
+
+	function setErrorHandler(f) {
+		__error.ref = f;
+		return f;
 	}
 
 	public function call( params : Array<Dynamic>, onData : Dynamic -> Void ) : Void {
@@ -52,7 +59,7 @@ class AsyncConnection implements Dynamic<AsyncConnection> {
 			var me = this;
 			var p = params.copy();
 			p.unshift({
-				onStatus : function(e) { me.onError(e); },
+				onStatus : function(e) { me.__error.ref(e); },
 				onResult : function(r) { onData(r); }
 			});
 			p.unshift(__path.join("."));
@@ -77,12 +84,12 @@ class AsyncConnection implements Dynamic<AsyncConnection> {
 				v = s.unserialize();
 			} catch( err : Dynamic ) {
 				ok = false;
-				me.onError(err);
+				me.__error.ref(err);
 			}
 			if( ok )
 				onData(v);
 		};
-		h.onError = onError;
+		h.onError = function(e) { me.__error.ref(e); };
 		h.request(true);
 	}
 

+ 11 - 11
std/haxe/remoting/AsyncDebugConnection.hx

@@ -24,40 +24,40 @@
  */
 package haxe;
 
-class AsyncDebugConnection implements AsyncConnection, implements Dynamic<AsyncDebugConnection> {
+class AsyncDebugConnection extends AsyncConnection, implements Dynamic<AsyncDebugConnection> {
 
-	var __data : Dynamic;
-	var __path : Array<String>; // not used there
 	var lastCalls : List<{ path : Array<String>, params : Array<Dynamic> }>;
 
 	public function new(cnx : AsyncConnection) {
-		__data = cnx;
+		super(cnx,[]);
 		lastCalls = new List();
-		onError = cnx.onError;
+		__error = cnx.__error;
+		setErrorHandler(__error.ref);
+	}
+
+	function setErrorHandler(f) {
 		var me = this;
-		cnx.onError = function(e) {
+		__error.ref = function(e) {
 			var l = me.lastCalls.pop();
 			if( l != null )
 				me.onErrorDisplay(l.path,l.params,e);
-			me.onError(e);
+			f(e);
 		}
+		return f;
 	}
 
 	function __resolve(field : String) : AsyncConnection {
 		var s = new AsyncDebugConnection(__data.__resolve(field));
 		s.lastCalls = lastCalls;
+		s.__error = __error;
 		// late binding of events
 		var me = this;
-		s.onError = function(e) { me.onError(e); };
 		s.onCall = function(p,pa) { me.onCall(p,pa); };
 		s.onResult = function(p,pa,r) { me.onResult(p,pa,r); };
 		s.onErrorDisplay = function(p,pa,e) { me.onErrorDisplay(p,pa,e); };
 		return s;
 	}
 
-	public function onError( err : Dynamic ) {
-	}
-
 	public function onErrorDisplay( path : Array<String>, params : Array<Dynamic>, err : Dynamic ) {
 		trace(path.join(".")+"("+params.join(",")+") = ERROR "+Std.string(err));
 	}

+ 5 - 5
std/haxe/remoting/SocketConnection.hx

@@ -34,8 +34,7 @@ class SocketConnection extends AsyncConnection {
 	function __resolve(field) : AsyncConnection {
 		var s = new SocketConnection(__data,__path.copy());
 		var me = this;
-		// late binding
-		s.onError = function(e) { me.onError(e); }
+		s.__error = __error;
 		s.__funs = __funs;
 		#if neko
 		s.__r = __r;
@@ -53,7 +52,7 @@ class SocketConnection extends AsyncConnection {
 			sendMessage(__data,s.toString());
 			__funs.add(onData);
 		} catch( e : Dynamic ) {
-			onError(e);
+			__error.ref(e);
 		}
 	}
 
@@ -148,7 +147,7 @@ class SocketConnection extends AsyncConnection {
 					return null;
 			}
 		} catch( e : Dynamic ) {
-			sc.onError(e);
+			sc.__error.ref(e);
 			return null;
 		}
 		if( f != null ) {
@@ -188,7 +187,7 @@ class SocketConnection extends AsyncConnection {
 				s.serialize(val);
 			sendMessage(sc.__data,s.toString());
 		} catch( e : Dynamic ) {
-			sc.onError(e);
+			sc.__error.ref(e);
 			return null;
 		}
 		if( exc )
@@ -220,6 +219,7 @@ class SocketConnection extends AsyncConnection {
 			t.run = function() {
 				t.stop();
 				var e = processMessage(sc,data.substr(2,data.length-2));
+				// error happened in response handler, not in request
 				if( e != null )
 					throw e.exc;
 			};