REFACTOR: Mitigate cyclic dependency between Jsonable classes (#1792)

This commit is contained in:
catloversg
2024-11-24 06:53:31 +07:00
committed by GitHub
parent 45a6ca6b8e
commit 05da0efc81
23 changed files with 121 additions and 114 deletions

View File

@@ -1,7 +1,6 @@
import type { WorkerScript } from "./WorkerScript";
import { ScriptDeath } from "./ScriptDeath";
import type { NetscriptContext } from "./APIWrapper";
import { dialogBoxCreate } from "../ui/React/DialogBox";
/** Log a message to a script's logs */
export function log(ctx: NetscriptContext, message: () => string) {
@@ -74,43 +73,3 @@ export function errorMessage(ctx: NetscriptContext, msg: string, type = "RUNTIME
return null;
}
}
/** Generate an error dialog when workerscript is known */
export function handleUnknownError(e: unknown, ws: WorkerScript | null = null, initialText = "") {
if (e instanceof ScriptDeath) {
// No dialog for ScriptDeath
return;
}
if (ws && typeof e === "string") {
const headerText = basicErrorMessage(ws, "", "");
if (!e.includes(headerText)) e = basicErrorMessage(ws, e);
} 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}`;
} else if (e instanceof Error) {
// Ignore any cancellation errors from Monaco that get here
if (e.name === "Canceled" && e.message === "Canceled") return;
const msg = `${e.message}${e.stack ? `\nstack:\n${e.stack.toString()}` : ""}`;
e = ws ? basicErrorMessage(ws, msg) : `RUNTIME ERROR:\n\n${msg}`;
}
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;
}
dialogBoxCreate(initialText + String(e));
}
/** Use this handler to handle the error when we call getSaveData function or getSaveInfo function */
export function handleGetSaveDataInfoError(error: unknown, fromGetSaveInfo = false) {
console.error(error);
let errorMessage = `Cannot get save ${fromGetSaveInfo ? "info" : "data"}. Error: ${error}.`;
if (error instanceof RangeError) {
errorMessage += " This may be because the save data is too large.";
}
if (error instanceof Error && error.stack) {
errorMessage += `\nStack:\n${error.stack}`;
}
dialogBoxCreate(errorMessage);
}

View File

@@ -10,7 +10,7 @@ import { GetServer } from "../Server/AllServers";
import { AddRecentScript } from "./RecentScripts";
import { ITutorial } from "../InteractiveTutorial";
import { AlertEvents } from "../ui/React/AlertManager";
import { handleUnknownError } from "./ErrorMessages";
import { handleUnknownError } from "../utils/ErrorHandler";
import { roundToTwo } from "../utils/helpers/roundToTwo";
export function killWorkerScript(ws: WorkerScript): boolean {