diff --git a/ui/common/src/common.ts b/ui/common/src/common.ts index 00e3e69a8090..25f01164acd1 100644 --- a/ui/common/src/common.ts +++ b/ui/common/src/common.ts @@ -100,8 +100,9 @@ export const requestIdleCallback = (f: () => void, timeout?: number): void => { else requestAnimationFrame(f); }; -export const escapeHtml = (str: string): string => - /[&<>"']/.test(str) +export function escapeHtml(str: string): string { + if (typeof str !== 'string') str = JSON.stringify(str); // throws + return /[&<>"']/.test(str) ? str .replace(/&/g, '&') .replace(/ .replace(/'/g, ''') .replace(/"/g, '"') : str; +} export function frag(html: string): T { const div = document.createElement('div'); diff --git a/ui/common/src/permalog.ts b/ui/common/src/permalog.ts index a26a537dfaea..3827cad916b0 100644 --- a/ui/common/src/permalog.ts +++ b/ui/common/src/permalog.ts @@ -53,20 +53,24 @@ function makeLog(): LichessLog { } const log: LichessLog = async (...args: any[]) => { - const msg = `#${site.info ? `${site.info.commit.substring(0, 7)} - ` : ''}${args - .map(stringify) - .join(' ')}`; - let nextKey = Date.now(); - console.log(...args); - if (nextKey === lastKey) { - nextKey += drift; - drift += 0.001; - } else { - drift = 0.001; - lastKey = nextKey; + try { + const msg = `#${site.info ? `${site.info.commit.substring(0, 7)} - ` : ''}${args + .map(stringify) + .join(' ')}`; + let nextKey = Date.now(); + console.log(...args); + if (nextKey === lastKey) { + nextKey += drift; + drift += 0.001; + } else { + drift = 0.001; + lastKey = nextKey; + } + await ready; + await store?.put(nextKey, msg); + } catch (e) { + console.error(e); } - await ready; - await store?.put(nextKey, msg); }; log.clear = async () => { @@ -96,11 +100,19 @@ function makeLog(): LichessLog { show: true, }); }); + window.addEventListener('unhandledrejection', async e => { - log(`${terseHref()} - ${e.reason}`); + let reason = e.reason; + if (typeof reason !== 'string') + try { + reason = JSON.stringify(e.reason); + } catch (_) { + reason = 'unhandled rejection, reason not a string'; + } + log(`${terseHref()} - ${reason}`); if (site.debug) domDialog({ - htmlText: escapeHtml(e.reason.toString()), + htmlText: escapeHtml(reason), class: 'debug', show: true, });