handle errors and timeouts when doing the jaxy
Ted Unangst tedu@tedunangst.com
Fri, 01 Apr 2022 15:40:45 -0400
1 files changed,
25 insertions(+),
4 deletions(-)
jump to
M
views/honkpage.js
→
views/honkpage.js
@@ -9,14 +9,19 @@ }
function post(url, data) { var x = new XMLHttpRequest() x.open("POST", url) + x.timeout = 30 * 1000 x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") x.send(data) } -function get(url, whendone) { +function get(url, whendone, whentimedout) { var x = new XMLHttpRequest() x.open("GET", url) + x.timeout = 5 * 1000 x.responseType = "json" x.onload = function() { whendone(x) } + if (whentimedout) { + x.ontimeout = function(e) { whentimedout(x, e) } + } x.send() } function bonk(el, xid) {@@ -160,10 +165,18 @@ var args = hydrargs()
var stash = curpagestate.name + ":" + curpagestate.arg args["tophid"] = tophid[stash] get("/hydra?" + encode(args), function(xhr) { - var lenhonks = fillinhonks(xhr, true) btn.innerHTML = "refresh" btn.disabled = false - refreshupdate(" " + lenhonks + " new") + if (xhr.status == 200) { + var lenhonks = fillinhonks(xhr, true) + refreshupdate(" " + lenhonks + " new") + } else { + refreshupdate(" status: " + xhr.status) + } + }, function(xhr, e) { + btn.innerHTML = "refresh" + btn.disabled = false + refreshupdate(" timed out") }) } function statechanger(evt) {@@ -203,7 +216,15 @@ } else {
// or create one and fill it honksonpage.prepend(document.createElement("div")) var args = hydrargs() - get("/hydra?" + encode(args), function(xhr) { fillinhonks(xhr, false) }) + get("/hydra?" + encode(args), function(xhr) { + if (xhr.status == 200) { + var lenhonks = fillinhonks(xhr, false) + } else { + refreshupdate(" status: " + xhr.status) + } + }, function(xhr, e) { + refreshupdate(" timed out") + }) } refreshupdate("") }