From d6cacdd4ecd6a61332ac15ab41805ad59c0345aa Mon Sep 17 00:00:00 2001 From: Paul Berberian Date: Wed, 19 Jun 2024 11:11:56 +0200 Subject: [PATCH] Client: Add Uncaught Errors reporting --- client/src/client.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/client/src/client.js b/client/src/client.js index 11b674a..8c7b512 100644 --- a/client/src/client.js +++ b/client/src/client.js @@ -127,6 +127,17 @@ function init(currentScriptSrc, playerClass) { }; }); + if (typeof window === "object" && window !== null) { + window.addEventListener("error", onGlobalError); + spyRemovers.push(() => { + window.removeEventListener("error", onGlobalError); + }); + window.addEventListener("unhandledrejection", onGlobalError); + spyRemovers.push(() => { + window.removeEventListener("unhandledrejection", onGlobalError); + }); + } + if (SHOULD_LOG_REQUESTS) { const originalXhrOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function () { @@ -221,6 +232,20 @@ function init(currentScriptSrc, playerClass) { : window.TextDecoder; const escape = window.escape; + /** + * Function to trigger when there's a global uncaught error, such as on window. + * @param {*} err + */ + function onGlobalError(err) { + if (err && err.error) { + formatAndSendLog("UncaughtError", processArg(err.error)); + } else if (err && err.reason) { + formatAndSendLog("UncaughtError", processArg(err.reason)); + } else { + formatAndSendLog("UncaughtError", processArg(err)); + } + } + /** * Creates a string from the given Uint8Array containing utf-8 code units. * @param {Uint8Array} bytes