From 83119a25c8860e10ee5f60b67e7db64953e061e3 Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 4 Apr 2022 16:31:20 +0530 Subject: [PATCH] better error handling in checkup --- www/assert/assertions.js | 70 +++++++++++++++++++++++++------ www/checkup/dependency-warning.js | 31 ++++++++++++-- www/checkup/index.html | 4 +- 3 files changed, 88 insertions(+), 17 deletions(-) diff --git a/www/assert/assertions.js b/www/assert/assertions.js index 8e2351cf7..d5f806e03 100644 --- a/www/assert/assertions.js +++ b/www/assert/assertions.js @@ -6,19 +6,65 @@ define([], function () { var MESSAGES = []; var assert = function (test, msg) { MESSAGES.push(msg || false); - ASSERTS.push(function (cb, i) { - test(function (result) { - if (result === true) { - passed++; - cb(); - } else { - cb({ - test: i, - message: msg, - output: result, - }); + ASSERTS.push(function (_cb, i) { + var called = false; + var to; + var cb = function (arg) { + if (to) { + clearTimeout(to); + to = undefined; } - }, msg); + + if (called) { return; } + called = true; + _cb(arg); + }; + + var _msg = document.createElement('span'); + _msg.innerText = "An unexpected error occurred. See your browser's console for more details"; + if (msg && !msg.innerText) { + msg.innerText = _msg.innerText; + } + + to = setTimeout(function () { + cb({ + test: i, + message: msg || _msg, + output: "TIMEOUT", + }); + }, 25000); + + try { + test(function (result) { + if (result === true) { + passed++; + cb(); + } else { + cb({ + test: i, + message: msg || _msg, + output: result, + }); + } + }, msg); + } catch (err) { + console.error(err); + msg.innerText = `Synchronous error thrown (${(err.message || err)}). See console for more details.`; + + //from ${err.fileName} line ${err.lineNumber}`; + cb({ + test: i, + message: msg || _msg, + output: { + message: err.message, + file: err.fileName, + line: err.lineNumber, + stack: typeof(err.stack) === 'string'? + err.stack.split('\n'): + err.stack, + }, + }); + } }); }; diff --git a/www/checkup/dependency-warning.js b/www/checkup/dependency-warning.js index 0b11fcf1e..f55a83b6a 100644 --- a/www/checkup/dependency-warning.js +++ b/www/checkup/dependency-warning.js @@ -1,4 +1,15 @@ (function () { + var h = (tag, children) => { + var el = document.createElement(tag); + children.forEach(child => { + if (typeof(child) === 'string') { + return void el.appendChild(document.createTextNode(child)); + } + el.appendChild(child); + }); + return el; + }; + var first = true; window.addEventListener('error', function (ev) { if (!ev) { return; } @@ -7,11 +18,25 @@ var nodeName = srcElement.nodeName; if (nodeName !== 'SCRIPT') { return; } var src = srcElement.src; - if (!/\/bower_components\/.*/.test(src)) { return; } + + if (/\/api\/.*/.test(src)) { + console.error("A serverside API endpoint could not be reached.", src); + } + + //if (!/\/bower_components\/.*/.test(src)) { return; } if (first) { - document.write(`

It's possible that this server's administrators forgot to install its client-side dependencies with 'bower update'.

`); + document.body.appendChild(h('h1', ['Oops!'])); + document.body.appendChild(h('p', [ + `It's possible that this server's administrators forgot to install its client-side dependencies with 'bower update',`, + ` or that some other required resource couldn't be loaded.`, + ` See your browser's console for more details.`, + ])); first = false; } - document.write(`

Failed to load ${src}.

`); + document.body.appendChild(h('p', [ + 'Failed to load ', + h('code', [src]), + '.', + ])); }, true); }()); diff --git a/www/checkup/index.html b/www/checkup/index.html index 9015d10f2..dfe69ca15 100644 --- a/www/checkup/index.html +++ b/www/checkup/index.html @@ -3,10 +3,10 @@ - -
+ +