FEATURE: Add "Recent Errors" tab and improved error modal (#2169)

This commit is contained in:
Michael Ficocelli
2025-06-16 19:43:21 -04:00
committed by GitHub
parent cf72937faf
commit 18f84396e2
17 changed files with 530 additions and 34 deletions
+52 -10
View File
@@ -1,8 +1,9 @@
import { basicErrorMessage } from "../Netscript/ErrorMessages";
import { ScriptDeath } from "../Netscript/ScriptDeath";
import type { WorkerScript } from "../Netscript/WorkerScript";
import { dialogBoxCreate } from "../ui/React/DialogBox";
import { getErrorMessageWithStackAndCause } from "./ErrorHelper";
import { getErrorMessageWithStackAndCause, parseUnknownError } from "./ErrorHelper";
import { DisplayError } from "../ErrorHandling/DisplayError";
/** Generate an error dialog when workerscript is known */
export function handleUnknownError(e: unknown, ws: WorkerScript | null = null, initialText = "") {
@@ -10,12 +11,36 @@ export function handleUnknownError(e: unknown, ws: WorkerScript | null = null, i
// No dialog for ScriptDeath
return;
}
const errorDetails = parseUnknownError(e);
if (ws && typeof e === "string") {
const headerText = basicErrorMessage(ws, "", "");
if (!e.includes(headerText)) e = basicErrorMessage(ws, e);
/**
* - Attempt to strip out the error type, if present.
* - Extract error text by skipping:
* - Error type
* - Script name and PID
*
* Error example:
* "RUNTIME ERROR\ntest.js@home (PID - 1)\n\ngetServer: Invalid hostname: 'invalid'\n\nStack:\ntest.js:L5@main"
*
* - errorType: "RUNTIME"
* - errorText: "getServer: Invalid hostname: 'invalid'\n\nStack:\ntest.js:L5@main"
*/
const errorType = e.match(/^(\w+) ERROR/)?.[1];
if (errorType) {
const errorText = e.split(/\n/).slice(3).join("\n");
DisplayError(initialText + errorText, errorType, ws.scriptRef.filename, ws.hostname, ws.pid);
return;
}
DisplayError(initialText + e, "RUNTIME", ws.scriptRef.filename, ws.hostname, ws.pid);
} else if (e instanceof SyntaxError) {
const msg = `${e.message} (sorry we can't be more helpful)`;
e = ws ? basicErrorMessage(ws, msg, "SYNTAX") : `SYNTAX ERROR:\n\n${msg}`;
DisplayError(
initialText + msg + (errorDetails.stack ?? ""),
"SYNTAX",
ws?.scriptRef?.filename,
ws?.hostname,
ws?.pid,
);
} else if (e instanceof Error) {
// Ignore any cancellation errors from Monaco that get here
if (e.name === "Canceled" && e.message === "Canceled") {
@@ -32,15 +57,13 @@ export function handleUnknownError(e: unknown, ws: WorkerScript | null = null, i
*/
console.error(e);
const msg = getErrorMessageWithStackAndCause(e);
e = ws ? basicErrorMessage(ws, msg) : `RUNTIME ERROR:\n\n${msg}`;
}
if (typeof e !== "string") {
DisplayError(initialText + msg, getErrorType(e.stack) ?? "RUNTIME", ws?.scriptRef?.filename, ws?.hostname, ws?.pid);
} else if (typeof e !== "string") {
console.error("Unexpected error:", e);
const msg = `Unexpected type of error thrown. This error was likely thrown manually within a script.
Error has been logged to the console.\n\nType of error: ${typeof e}\nValue of error: ${e}`;
e = ws ? basicErrorMessage(ws, msg, "UNKNOWN") : msg;
DisplayError(msg, "UNKNOWN", ws?.scriptRef?.filename, ws?.hostname, ws?.pid);
}
dialogBoxCreate(initialText + String(e));
}
/** Use this handler to handle the error when we call getSaveData function or getSaveInfo function */
@@ -55,3 +78,22 @@ export function handleGetSaveDataInfoError(error: unknown, fromGetSaveInfo = fal
}
dialogBoxCreate(errorMessage);
}
function getErrorType(e = ""): string | undefined {
if (e.toLowerCase().includes("typeerror")) {
return "TYPE";
}
if (e.toLowerCase().includes("syntaxerror")) {
return "SYNTAX";
}
if (e.toLowerCase().includes("referenceerror")) {
return "REFERENCE";
}
if (e.toLowerCase().includes("rangeerror")) {
return "RANGE";
}
// Check if the first line contains an error type
const match = e.match(/^\s*([A-Z]+)\s+ERROR/);
return match?.[1];
}