Handle Errors and single nodes in writeLog

writeLog passed anything non-string straight to h() as its children argument, which h requires to be an array or a string - so writeError(err) threw "Third argument must be an array or string" and swallowed the error it was called with. Denying camera permission on /QRCodeScan hit this: the detect() rejection is a DOMException, and the TypeError went to the console instead of the message going to the page log. Calc, Concat, Letterflixd and Storage all call writeError with non-strings too. Normalise the argument through logChildren first: a string stays as it was, a Node is wrapped in the array h wants, an array of nodes passes through (logQRValue relies on that), and anything else - Error, DOMException - logs its message, falling back to String(msg). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

author
Marijn Besseling <njirambem@gmail.com> · 2026-09-09 19:43 UTC
committer
Marijn Besseling <njirambem@gmail.com> · 2026-09-09 19:44 UTC
commit
b3393efd631bed0f7620ffc28179e0f2b1b992b9
parent
354201a7a4
tree
browse at this commit

1 file changed +20 -6

Blog/wwwroot/common.module.js +20 -6

@@ -230,14 +230,28 @@ function writeDebug(msg) {
230 230 writeLog(msg, "debug");
231 231 }
232 232
233 -function writeLog(msg, className) {
233 +/**
234 + * Normalise anything writeLog is handed into children `h` accepts.
235 + * @param {string|String|Node|Array<HTMLElement|Text|string>|Error|unknown} msg
236 + * @returns {Array<HTMLElement|Text|string>}
237 + */
238 +function logChildren(msg) {
234 239 if (typeof msg === "string" || msg instanceof String) {
235 - // log.appendChild(div(addClass(span(msg), className)));
236 - log.appendChild(h("div", {class: className}, [h("span", msg)]));
237 - } else {
238 - log.appendChild(h("div", {class: className}, msg));
239 - // log.appendChild(div(addClass(msg, className)));
240 + return [h("span", String(msg))];
241 + }
242 + if (msg instanceof Node) {
243 + return [msg];
240 244 }
245 + if (Array.isArray(msg)) {
246 + return msg;
247 + }
248 + // Error, DOMException, or anything else: show its message.
249 + const message = typeof msg?.message === "string" ? msg.message : String(msg);
250 + return [h("span", message)];
251 +}
252 +
253 +function writeLog(msg, className) {
254 + log.appendChild(h("div", {class: className}, logChildren(msg)));
241 255 }
242 256
243 257 function resetLog() {