diff --git a/src/NetscriptWorker.ts b/src/NetscriptWorker.ts index c08592f86..82b8c6861 100644 --- a/src/NetscriptWorker.ts +++ b/src/NetscriptWorker.ts @@ -126,6 +126,7 @@ function startNetscript2Script(workerScript: WorkerScript): Promise reject(e)); }).catch((e) => { + console.log(e); if (e instanceof Error) { if (e instanceof SyntaxError) { workerScript.errorMessage = makeRuntimeRejectMsg(workerScript, e.message + " (sorry we can't be more helpful)"); diff --git a/src/UncaughtPromiseHandler.ts b/src/UncaughtPromiseHandler.ts new file mode 100644 index 000000000..5777fcb06 --- /dev/null +++ b/src/UncaughtPromiseHandler.ts @@ -0,0 +1,18 @@ +import { isScriptErrorMessage } from "./NetscriptEvaluator"; +import { dialogBoxCreate } from "./ui/React/DialogBox"; + +export function setupUncaughtPromiseHandler(): void { + window.addEventListener("unhandledrejection", function (e) { + if (isScriptErrorMessage(e.reason)) { + const errorTextArray = e.reason.split("|DELIMITER|"); + const hostname = errorTextArray[1]; + const scriptName = errorTextArray[2]; + const errorMsg = errorTextArray[3]; + + let msg = `UNCAUGHT PROMISE ERROR
You forgot to await a promise
${scriptName}@${hostname}
`; + msg += "
"; + msg += errorMsg; + dialogBoxCreate(msg); + } + }); +} diff --git a/src/engine.tsx b/src/engine.tsx index 1b8788520..3d2545406 100644 --- a/src/engine.tsx +++ b/src/engine.tsx @@ -46,6 +46,7 @@ import { exceptionAlert } from "./utils/helpers/exceptionAlert"; import { startExploits } from "./Exploits/loops"; import React from "react"; +import { setupUncaughtPromiseHandler } from "./UncaughtPromiseHandler"; const Engine: { _lastUpdate: number; @@ -237,6 +238,7 @@ const Engine: { load: function (saveString) { startExploits(); + setupUncaughtPromiseHandler(); // Load game from save or create new game if (loadGame(saveString)) { ThemeEvents.emit();