fix uncaught promise

This commit is contained in:
Olivier Gagnon 2021-12-20 13:57:07 -05:00
parent 5dc9ac040a
commit e9886cc6bc
3 changed files with 21 additions and 0 deletions

@ -126,6 +126,7 @@ function startNetscript2Script(workerScript: WorkerScript): Promise<WorkerScript
})
.catch((e) => 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)");

@ -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<br>You forgot to await a promise<br>${scriptName}@${hostname}<br>`;
msg += "<br>";
msg += errorMsg;
dialogBoxCreate(msg);
}
});
}

@ -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();