diff options
author | 2009-04-16 13:47:20 -0400 | |
---|---|---|
committer | 2009-04-16 13:47:20 -0400 | |
commit | 6ada30a32e69cf288e50ee0ca2963e031c588ee0 (patch) | |
tree | d0d08dc15edc8e7dc1f916a592160f8c9e561bd2 /lib/js/urweb.js | |
parent | e65c023309173aa291952143537f174888a8de86 (diff) |
Some client-side error handling
Diffstat (limited to 'lib/js/urweb.js')
-rw-r--r-- | lib/js/urweb.js | 168 |
1 files changed, 109 insertions, 59 deletions
diff --git a/lib/js/urweb.js b/lib/js/urweb.js index 534769a5..877ed77f 100644 --- a/lib/js/urweb.js +++ b/lib/js/urweb.js @@ -23,6 +23,79 @@ function length(ls) { } +// Error handling + +function whine(msg) { + alert(msg); + throw msg; +} + +function pf() { + whine("Pattern match failure"); +} + +function runHandlers(ls, arg) { + for (; ls; ls = ls.next) + try { + ls.data(arg)(null); + } catch (v) { } +} + +var errorHandlers = null; + +function onError(f) { + errorHandlers = cons(f, errorHandlers); +} + +function er(s) { + runHandlers(errorHandlers, s); + throw {uw_error: s}; +} + +var failHandlers = null; + +function onFail(f) { + failHandlers = cons(f, failHandlers); +} + +function doExn(v) { + if (v == null || v.uw_error == null) { + var s = (v == null ? "null" : v.toString()); + runHandlers(failHandlers, s); + } +} + +var disconnectHandlers = null; + +function onDisconnect(f) { + disconnectHandlers = cons(function (_){return f}, disconnectHandlers); +} + +function discon() { + runHandlers(disconnectHandlers, null); +} + +var connectHandlers = null; + +function onConnectFail(f) { + connectHandlers = cons(function (_){return f}, connectHandlers); +} + +function conn() { + runHandlers(connectHandlers, null); +} + +var serverHandlers = null; + +function onServerError(f) { + serverHandlers = cons(f, serverHandlers); +} + +function servErr(s) { + runHandlers(serverHandlers, s); +} + + // Embedding closures in XML strings function cs(f) { @@ -90,19 +163,23 @@ function flattenLocal(s) { function populate(node) { var s = node.signal; var oldSources = node.sources; - var sr = s(); - var newSources = sr.sources; + try { + var sr = s(); + var newSources = sr.sources; - for (var sp = oldSources; sp; sp = sp.next) - if (!member(sp.data, newSources)) - sp.data.dyns = remove(node, sp.data.dyns); + for (var sp = oldSources; sp; sp = sp.next) + if (!member(sp.data, newSources)) + sp.data.dyns = remove(node, sp.data.dyns); - for (var sp = newSources; sp; sp = sp.next) - if (!member(sp.data, oldSources)) - sp.data.dyns = cons(node, sp.data.dyns); + for (var sp = newSources; sp; sp = sp.next) + if (!member(sp.data, oldSources)) + sp.data.dyns = cons(node, sp.data.dyns); - node.sources = newSources; - node.recreate(sr.data); + node.sources = newSources; + node.recreate(sr.data); + } catch (v) { + doExn(v); + } } function sc(v) { @@ -160,7 +237,11 @@ function runScripts(node) { scriptsCopy[i] = scripts[i]; for (var i = 0; i < len; ++i) { thisScript = scriptsCopy[i]; - eval(thisScript.textContent); + try { + eval(thisScript.textContent); + } catch (v) { + doExn(v); + } } thisScript = savedScript; @@ -227,7 +308,7 @@ function pi(s) { if (r.toString() == s) return r; else - throw "Can't parse int: " + s; + er("Can't parse int: " + s); } function pfl(s) { @@ -235,7 +316,7 @@ function pfl(s) { if (r.toString() == s) return r; else - throw "Can't parse float: " + s; + er("Can't parse float: " + s); } function uf(s) { @@ -247,43 +328,6 @@ function uu(s) { } -// Error handling - -function whine(msg) { - alert(msg); - throw msg; -} - -function pf() { - whine("Pattern match failure"); -} - -var errorHandlers = null; - -function onError(f) { - errorHandlers = cons(f, errorHandlers); -} - -function er(s) { - for (var ls = errorHandlers; ls; ls = ls.next) - ls.data(s)(null); - throw {uw_error: s}; -} - -var failHandlers = null; - -function onFail(f) { - failHandlers = cons(f, failHandlers); -} - -function doExn(v) { - if (v == null || v.uw_error == null) { - var s = (v == null ? "null" : v.toString()); - for (var ls = failHandlers; ls; ls = ls.next) - ls.data(s)(null); - } -} - // Remote calls @@ -333,10 +377,14 @@ function rc(uri, parse, k) { isok = true; } catch (e) { } - if (isok) - k(parse(xhr.responseText)); - else { - whine("Error querying remote server!"); + if (isok) { + try { + k(parse(xhr.responseText)); + } catch (v) { + doExn(v); + } + } else { + conn(); } } }; @@ -406,8 +454,10 @@ function listener() { if (isok) { var lines = xhr.responseText.split("\n"); - if (lines.length < 2) - return; // throw "Empty message from remote server"; + if (lines.length < 2) { + discon(); + return; + } for (var i = 0; i+1 < lines.length; i += 2) { var chn = lines[i]; @@ -439,9 +489,9 @@ function listener() { connect(); } else { - /*try { - whine("Error querying remote server for messages! " + xhr.status); - } catch (e) { }*/ + try { + servError("Error querying remote server for messages: " + xhr.status); + } catch (e) { servError("Error querying remote server for messages"); } } } }; |