From e9886cc6bca34d518e4ddc48ae1674b29ef528a0 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Mon, 20 Dec 2021 13:57:07 -0500 Subject: [PATCH] fix uncaught promise --- src/NetscriptWorker.ts | 1 + src/UncaughtPromiseHandler.ts | 18 ++++++++++++++++++ src/engine.tsx | 2 ++ 3 files changed, 21 insertions(+) create mode 100644 src/UncaughtPromiseHandler.ts 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();