From da2b458cd3436840830486f3e591392c71e4269f Mon Sep 17 00:00:00 2001 From: Massimo Federico Bonfigli Date: Sat, 10 Aug 2019 13:48:42 +0200 Subject: [PATCH 1/2] Added call to error callback function in case requested method is not bound to the channel. --- src/jschannel.js | 119 ++++++++++++++++++++++++----------------------- 1 file changed, 61 insertions(+), 58 deletions(-) diff --git a/src/jschannel.js b/src/jschannel.js index a4f6ba2..37ecbf0 100644 --- a/src/jschannel.js +++ b/src/jschannel.js @@ -359,71 +359,74 @@ // now, what type of message is this? if (m.id && method) { - // a request! do we have a registered handler for this request? - if (regTbl[method]) { - var trans = createTransaction(m.id, origin, m.callbacks ? m.callbacks : [ ]); - inTbl[m.id] = { }; - try { - // callback handling. we'll magically create functions inside the parameter list for each - // callback - if (m.callbacks && s_isArray(m.callbacks) && m.callbacks.length > 0) { - for (var i = 0; i < m.callbacks.length; i++) { - var path = m.callbacks[i]; - var obj = m.params; - var pathItems = path.split('/'); - for (var j = 0; j < pathItems.length - 1; j++) { - var cp = pathItems[j]; - if (typeof obj[cp] !== 'object') obj[cp] = { }; - obj = obj[cp]; - } - obj[pathItems[pathItems.length - 1]] = (function() { - var cbName = path; - return function(params) { - return trans.invoke(cbName, params); - }; - })(); + // a request! + var trans = createTransaction(m.id, origin, m.callbacks ? m.callbacks : [ ]); + inTbl[m.id] = { }; + try { + // do we have a registered handler for this request? + if (!regTbl[method]) { + // Oh no! We don't have a registered handler. Maybe it is not yet bound, so better notify of the error. + throw {"error":"method_not_bound_error", "message":"the requested method is not bound on the channel"} + } + // callback handling. we'll magically create functions inside the parameter list for each + // callback + if (m.callbacks && s_isArray(m.callbacks) && m.callbacks.length > 0) { + for (var i = 0; i < m.callbacks.length; i++) { + var path = m.callbacks[i]; + var obj = m.params; + var pathItems = path.split('/'); + for (var j = 0; j < pathItems.length - 1; j++) { + var cp = pathItems[j]; + if (typeof obj[cp] !== 'object') obj[cp] = { }; + obj = obj[cp]; } + obj[pathItems[pathItems.length - 1]] = (function() { + var cbName = path; + return function(params) { + return trans.invoke(cbName, params); + }; + })(); } - var resp = regTbl[method](trans, m.params); - if (!trans.delayReturn() && !trans.completed()) trans.complete(resp); - } catch(e) { - // automagic handling of exceptions: - var error = "runtime_error"; - var message = null; - // * if it's a string then it gets an error code of 'runtime_error' and string is the message - if (typeof e === 'string') { - message = e; - } else if (typeof e === 'object') { - // either an array or an object - // * if it's an array of length two, then array[0] is the code, array[1] is the error message - if (e && s_isArray(e) && e.length == 2) { - error = e[0]; - message = e[1]; - } - // * if it's an object then we'll look form error and message parameters - else if (typeof e.error === 'string') { - error = e.error; - if (!e.message) message = ""; - else if (typeof e.message === 'string') message = e.message; - else e = e.message; // let the stringify/toString message give us a reasonable verbose error string - } + } + var resp = regTbl[method](trans, m.params); + if (!trans.delayReturn() && !trans.completed()) trans.complete(resp); + } catch(e) { + // automagic handling of exceptions: + var error = "runtime_error"; + var message = null; + // * if it's a string then it gets an error code of 'runtime_error' and string is the message + if (typeof e === 'string') { + message = e; + } else if (typeof e === 'object') { + // either an array or an object + // * if it's an array of length two, then array[0] is the code, array[1] is the error message + if (e && s_isArray(e) && e.length == 2) { + error = e[0]; + message = e[1]; + } + // * if it's an object then we'll look form error and message parameters + else if (typeof e.error === 'string') { + error = e.error; + if (!e.message) message = ""; + else if (typeof e.message === 'string') message = e.message; + else e = e.message; // let the stringify/toString message give us a reasonable verbose error string } + } - // message is *still* null, let's try harder - if (message === null) { - try { - message = JSON.stringify(e); - /* On MSIE8, this can result in 'out of memory', which - * leaves message undefined. */ - if (typeof(message) == 'undefined') - message = e.toString(); - } catch (e2) { + // message is *still* null, let's try harder + if (message === null) { + try { + message = JSON.stringify(e); + /* On MSIE8, this can result in 'out of memory', which + * leaves message undefined. */ + if (typeof(message) == 'undefined') message = e.toString(); - } + } catch (e2) { + message = e.toString(); } - - trans.error(error,message); } + + trans.error(error,message); } } else if (m.id && m.callback) { if (!outTbl[m.id] ||!outTbl[m.id].callbacks || !outTbl[m.id].callbacks[m.callback]) From bb79c9a9916efd58f847c0cc9a4ae89720345662 Mon Sep 17 00:00:00 2001 From: Massimo Federico Bonfigli Date: Sat, 10 Aug 2019 13:57:18 +0200 Subject: [PATCH 2/2] Added an example of error callback in case of method not bound to the channel --- example/child.html | 9 +++++++++ example/parent.html | 25 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/example/child.html b/example/child.html index fcab9fb..ad7fee2 100644 --- a/example/child.html +++ b/example/child.html @@ -28,6 +28,15 @@ a.parseComplete(); }); +setTimeout(function(){ + chan.bind("delayedReverse", function(trans, s) { + if (typeof s !== 'string') { + throw [ "invalid_arguments", 'argument to reverse function should be a string' ]; + } + return s.split("").reverse().join(""); + }); +}, 500) + diff --git a/example/parent.html b/example/parent.html index 968c839..7035bc2 100644 --- a/example/parent.html +++ b/example/parent.html @@ -56,9 +56,15 @@

JSChannel example

Next, here's notification output:
-Finally, here's callback invocation output: +Now, here's callback invocation output:
+Next, here's error handling in case of method not bound yet: +
+ +Finally, let's try to call the same method after it has been bound: +
+