Unify error handling

This commit is contained in:
Snarling
2022-08-28 05:33:38 -04:00
parent 8dd507883a
commit 5798c4c7d3
21 changed files with 149 additions and 324 deletions
+23 -7
View File
@@ -1,13 +1,29 @@
import { AlertEvents } from "./AlertManager";
import React from "react";
import { SxProps } from "@mui/system";
import { Typography } from "@mui/material";
import { ScriptDeath } from "../../Netscript/ScriptDeath";
export function dialogBoxCreate(txt: string | JSX.Element, styles?: SxProps): void {
if (typeof txt !== "string") {
AlertEvents.emit(txt);
} else {
AlertEvents.emit(<Typography component="span" sx={styles} dangerouslySetInnerHTML={{ __html: txt }} />);
}
export function dialogBoxCreate(txt: string | JSX.Element): void {
AlertEvents.emit(typeof txt === "string" ? <Typography component="span">{txt}</Typography> : txt);
}
export function errorDialog(e: unknown, initialText = "") {
let errorText = "";
if (typeof e === "string") errorText = e;
else if (e instanceof ScriptDeath) {
if (!e.errorMessage) return; //No need for a dialog for an empty ScriptDeath
errorText = e.errorMessage;
} else if (e instanceof SyntaxError) errorText = e.message + " (sorry we can't be more helpful)";
else if (e instanceof Error) errorText = e.message + (e.stack ? `\nstack:\n${e.stack.toString()}` : "");
else {
errorText = "An unknown error was thrown, see console.";
console.error(e);
}
if (!initialText) {
if (e instanceof ScriptDeath) initialText = `${e.name}@${e.hostname} (PID - ${e.pid})\n\n`;
}
dialogBoxCreate(initialText + errorText);
}