Less hardcoding

This commit is contained in:
Snarling 2022-09-05 08:56:39 -04:00
parent b8496b4c2c
commit 8d0347577d
3 changed files with 14 additions and 9 deletions

@ -40,7 +40,7 @@ function wrapFunction(
const functionPath = tree.join(".");
const functionName = tree.pop();
if (typeof functionName !== "string") {
throw helpers.makeBasicErrorMsg(workerScript, "Failure occured while wrapping netscript api");
throw helpers.makeBasicErrorMsg(workerScript, "Failure occured while wrapping netscript api", "INITIALIZATION");
}
const ctx = {
workerScript,

@ -131,11 +131,11 @@ function argsToString(args: unknown[]): string {
}
/** Creates an error message string containing hostname, scriptname, and the error message msg */
function makeBasicErrorMsg(workerScript: WorkerScript, msg: string): string {
function makeBasicErrorMsg(workerScript: WorkerScript, msg: string, type = "RUNTIME"): string {
for (const scriptUrl of workerScript.scriptRef.dependencies) {
msg = msg.replace(new RegExp(scriptUrl.url, "g"), scriptUrl.filename);
}
return msg;
return `${type} ERROR\n${workerScript.name}@${workerScript.hostname} (PID - ${workerScript.pid})\n\n${msg}`;
}
/** Creates an error message string with a stack trace. */
@ -204,7 +204,7 @@ function makeRuntimeErrorMsg(ctx: NetscriptContext, msg: string): string {
}
log(ctx, () => msg);
let rejectMsg = `RUNTIME ERROR\n${ws.name}@${ws.hostname} (PID - ${ws.pid})\n\n${caller}: ${msg}`;
let rejectMsg = `${caller}: ${msg}`;
if (userstack.length !== 0) rejectMsg += `\n\nStack:\n${userstack.join("\n")}`;
return makeBasicErrorMsg(ws, rejectMsg);
}

@ -33,6 +33,7 @@ import { simple as walksimple } from "acorn-walk";
import { areFilesEqual } from "./Terminal/DirectoryHelpers";
import { Terminal } from "./Terminal";
import { ScriptArg } from "./Netscript/ScriptArg";
import { helpers } from "./Netscript/NetscriptHelpers";
export const NetscriptPorts: Map<number, IPort> = new Map();
@ -346,12 +347,16 @@ function createAndAddWorkerScript(runningScriptObj: RunningScript, server: BaseS
workerScript.log("", () => "Script finished running");
})
.catch(function (e) {
const initialText = `ERROR\n${workerScript.name}@${workerScript.hostname} (PID - ${workerScript.pid})\n\n`;
if (e instanceof SyntaxError) e = `SYNTAX ${initialText}${e.message} (sorry we can't be more helpful)`;
else if (e instanceof Error) {
e = `RUNTIME ${initialText}${e.message}${e.stack ? `\nstack:\n${e.stack.toString()}` : ""}`;
if (typeof e === "string") {
const headerText = helpers.makeBasicErrorMsg(workerScript, "", "");
//Add header info if it is not present;
if (!e.includes(headerText)) e = helpers.makeBasicErrorMsg(workerScript, e);
} else if (e instanceof SyntaxError) {
e = helpers.makeBasicErrorMsg(workerScript, `${e.message} (sorry we can't be more helpful)`, "SYNTAX");
} else if (e instanceof Error) {
e = helpers.makeBasicErrorMsg(workerScript, `${e.message}${e.stack ? `\nstack:\n${e.stack.toString()}` : ""}`);
}
errorDialog(e, typeof e === "string" && e.includes(initialText) ? "" : initialText);
errorDialog(e);
workerScript.log("", () => (e instanceof ScriptDeath ? "Script killed." : "Script crashed due to an error."));
killWorkerScript(workerScript);
});