Error reporting improvements

Arbitraty context can be added to reports of known errors by wrapping the problematic code in the debugWithContext function
main
peshomir 2025-07-14 12:26:30 +03:00
parent d83d50b5ae
commit bb36014f55
4 changed files with 44 additions and 19 deletions

View File

@ -7,7 +7,7 @@ export default (/** @type {ModUtils} */ modUtils) => {
modUtils.insertCode(
`window.removeEventListener("error", err);
msg = e.lineno + " " + e.colno + "|" + getStack(e); /* here */`,
`__fx.utils.reportError(e, msg);
`__fx.reportError(e, msg);
return alert("Error:\\n" + e.filename + " " + e.lineno + " " + e.colno + " " + e.message);`
)

40
src/debugging.js 100644
View File

@ -0,0 +1,40 @@
import { getVar } from "./gameInterface.js";
let debugContext = null;
export function reportError(e, message) {
function tryGetVar(name) {
try { return getVar(name) }
catch (error) { return error.toString(); }
}
message = e.filename + " " + e.lineno + " " + e.colno + " " + e.message + "\n" + message;
fetch("https://fx.peshomir.workers.dev/stats/errors", {
body: JSON.stringify({
message,
context: {
debug: debugContext,
gameState: tryGetVar("gameState"),
singleplayer: tryGetVar("gIsSingleplayer"),
swState: navigator.serviceWorker?.controller?.state,
location: window.location.toString(),
userAgent: navigator.userAgent,
dictionary: JSON.stringify(dictionary),
buildTimestamp,
scripts: Array.from(document.scripts).map(s => s.src)
}
}),
method: "POST"
}).catch(e => alert("Failed to report error: " + e));
}
export function debugWithContext(callback, context) {
try {
return callback();
} catch (error) {
debugContext = context;
setTimeout(() => {
if (debugContext !== null) debugContext = null;
});
throw error;
}
}

View File

@ -22,22 +22,5 @@ function textStyleBasedOnDensity(playerID) {
const playerBalances = getVar("playerBalances"), playerTerritories = getVar("playerTerritories");
return `hsl(${playerBalances[playerID] / (playerTerritories[playerID] * 1.5)}, 100%, 50%, 1)`;
}
function reportError(e, message) {
message = e.filename + " " + e.lineno + " " + e.colno + " " + e.message + "\n" + message;
fetch("https://fx.peshomir.workers.dev/stats/errors", {
body: JSON.stringify({
message,
context: {
swState: navigator.serviceWorker?.controller?.state,
location: window.location.toString(),
userAgent: navigator.userAgent,
dictionary,
buildTimestamp,
scripts: Array.from(document.scripts).map(s => s.src)
}
}),
method: "POST"
}).catch(e => alert("Failed to report error: " + e));
}
export default { getMaxTroops, getDensity, isPointInRectangle, fillTextMultiline, textStyleBasedOnDensity, reportError }
export default { getMaxTroops, getDensity, isPointInRectangle, fillTextMultiline, textStyleBasedOnDensity }

View File

@ -23,6 +23,7 @@ import hoveringTooltip from "./hoveringTooltip.js";
import { keybindFunctions, keybindHandler, mobileKeybinds } from "./keybinds.js";
import customLobby from './customLobby.js';
import { displayChangelog } from './changelog.js';
import { reportError } from './debugging.js';
const savedVersion = localStorage.getItem("fx_version");
if (savedVersion !== version) {
@ -42,6 +43,7 @@ __fx.keybindFunctions = keybindFunctions;
__fx.keybindHandler = keybindHandler;
__fx.mobileKeybinds = mobileKeybinds;
__fx.donationsTracker = donationsTracker;
__fx.reportError = reportError;
__fx.playerList = playerList;
__fx.hoveringTooltip = hoveringTooltip;
__fx.clanFilter = clanFilter;