Mercurial > urweb
diff lib/js/urweb.js @ 729:7c6b6c3c7b79
Some client-side error handling
author | Adam Chlipala <adamc@hcoop.net> |
---|---|
date | Thu, 16 Apr 2009 13:47:20 -0400 |
parents | 2197f0e24a9f |
children | 796e42c93c48 |
line wrap: on
line diff
--- a/lib/js/urweb.js Thu Apr 16 13:00:40 2009 -0400 +++ b/lib/js/urweb.js Thu Apr 16 13:47:20 2009 -0400 @@ -23,6 +23,79 @@ } +// 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 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 @@ 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 @@ 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 @@ 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 @@ } -// 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 @@ 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 @@ 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 @@ 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"); } } } };