Skip to content

Commit

Permalink
Client: Add Uncaught Errors reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
peaBerberian committed Jun 19, 2024
1 parent 1eeffea commit d6cacdd
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d6cacdd

Please sign in to comment.