From bc51733fbe58adc3de4c11aabc8a700dfdff4822 Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Tue, 22 Oct 2024 10:36:29 +0700 Subject: [PATCH] MISC: Improve exception alert (#1709) --- src/Electron.tsx | 4 +- src/NetscriptFunctions/UserInterface.ts | 4 +- src/Sidebar/ui/SidebarRoot.tsx | 4 +- src/Terminal/Terminal.ts | 6 +-- src/engine.tsx | 2 +- src/ui/LoadingScreen.tsx | 4 +- src/utils/ErrorHelper.ts | 12 +++--- .../hash.ts => utils/helpers/commitHash.ts} | 2 +- src/utils/helpers/exceptionAlert.ts | 40 ------------------- src/utils/helpers/exceptionAlert.tsx | 28 +++++++++++++ 10 files changed, 47 insertions(+), 59 deletions(-) rename src/{hash/hash.ts => utils/helpers/commitHash.ts} (67%) delete mode 100644 src/utils/helpers/exceptionAlert.ts create mode 100644 src/utils/helpers/exceptionAlert.tsx diff --git a/src/Electron.tsx b/src/Electron.tsx index 5ea2a3a9fc..5d2b431130 100644 --- a/src/Electron.tsx +++ b/src/Electron.tsx @@ -9,7 +9,7 @@ import { GetServer } from "./Server/AllServers"; import { ImportPlayerData, ElectronGameData, saveObject } from "./SaveObject"; import { exportScripts } from "./Terminal/commands/download"; import { CONSTANTS } from "./Constants"; -import { hash } from "./hash/hash"; +import { commitHash } from "./utils/helpers/commitHash"; import { resolveFilePath } from "./Paths/FilePath"; import { hasScriptExtension } from "./Paths/ScriptFilePath"; import { handleGetSaveDataError } from "./Netscript/ErrorMessages"; @@ -230,7 +230,7 @@ export function pushGameReady(): void { }, game: { version: CONSTANTS.VersionString, - hash: hash(), + hash: commitHash(), }, }); } diff --git a/src/NetscriptFunctions/UserInterface.ts b/src/NetscriptFunctions/UserInterface.ts index 6e677ad938..3d0577afa8 100644 --- a/src/NetscriptFunctions/UserInterface.ts +++ b/src/NetscriptFunctions/UserInterface.ts @@ -4,7 +4,7 @@ import { ThemeEvents } from "../Themes/ui/Theme"; import { defaultTheme } from "../Themes/Themes"; import { defaultStyles } from "../Themes/Styles"; import { CONSTANTS } from "../Constants"; -import { hash } from "../hash/hash"; +import { commitHash } from "../utils/helpers/commitHash"; import { InternalAPI, NetscriptContext } from "../Netscript/APIWrapper"; import { Terminal } from "../../src/Terminal"; import { helpers } from "../Netscript/NetscriptHelpers"; @@ -116,7 +116,7 @@ export function NetscriptUserInterface(): InternalAPI { getGameInfo: () => () => { const version = CONSTANTS.VersionString; - const commit = hash(); + const commit = commitHash(); const platform = navigator.userAgent.toLowerCase().includes(" electron/") ? "Steam" : "Browser"; const gameInfo = { diff --git a/src/Sidebar/ui/SidebarRoot.tsx b/src/Sidebar/ui/SidebarRoot.tsx index cdf2cff07b..8b7278a717 100644 --- a/src/Sidebar/ui/SidebarRoot.tsx +++ b/src/Sidebar/ui/SidebarRoot.tsx @@ -52,7 +52,7 @@ import { AugmentationName } from "@enums"; import { ProgramsSeen } from "../../Programs/ui/ProgramsRoot"; import { InvitationsSeen } from "../../Faction/ui/FactionsRoot"; -import { hash } from "../../hash/hash"; +import { commitHash } from "../../utils/helpers/commitHash"; import { Locations } from "../../Locations/Locations"; import { useCycleRerender } from "../../ui/React/hooks"; import { playerHasDiscoveredGo } from "../../Go/effects/effect"; @@ -277,7 +277,7 @@ export function SidebarRoot(props: { page: Page }): React.ReactElement { + Bitburner v{CONSTANTS.VersionString} } diff --git a/src/Terminal/Terminal.ts b/src/Terminal/Terminal.ts index 3fce2c7cc5..235be962d9 100644 --- a/src/Terminal/Terminal.ts +++ b/src/Terminal/Terminal.ts @@ -73,7 +73,7 @@ import { unalias } from "./commands/unalias"; import { vim } from "./commands/vim"; import { weaken } from "./commands/weaken"; import { wget } from "./commands/wget"; -import { hash } from "../hash/hash"; +import { commitHash } from "../utils/helpers/commitHash"; import { apr1 } from "./commands/apr1"; import { changelog } from "./commands/changelog"; import { clear } from "./commands/clear"; @@ -139,7 +139,7 @@ export class Terminal { commandHistoryIndex = 0; outputHistory: (Output | Link | RawOutput)[] = [ - new Output(`Bitburner v${CONSTANTS.VersionString} (${hash()})`, "primary"), + new Output(`Bitburner v${CONSTANTS.VersionString} (${commitHash()})`, "primary"), ]; // True if a Coding Contract prompt is opened @@ -613,7 +613,7 @@ export class Terminal { } clear(): void { - this.outputHistory = [new Output(`Bitburner v${CONSTANTS.VersionString} (${hash()})`, "primary")]; + this.outputHistory = [new Output(`Bitburner v${CONSTANTS.VersionString} (${commitHash()})`, "primary")]; TerminalEvents.emit(); TerminalClearEvents.emit(); } diff --git a/src/engine.tsx b/src/engine.tsx index 657d1f1d60..3bc0fe9f6e 100644 --- a/src/engine.tsx +++ b/src/engine.tsx @@ -216,7 +216,7 @@ const Engine: { try { Player.bladeburner.process(); } catch (e) { - exceptionAlert("Exception caught in Bladeburner.process(): " + e); + exceptionAlert(e); } } Engine.Counters.mechanicProcess = 5; diff --git a/src/ui/LoadingScreen.tsx b/src/ui/LoadingScreen.tsx index 729e169ab2..cc5bd49726 100644 --- a/src/ui/LoadingScreen.tsx +++ b/src/ui/LoadingScreen.tsx @@ -9,7 +9,7 @@ import { GameRoot } from "./GameRoot"; import { CONSTANTS } from "../Constants"; import { ActivateRecoveryMode } from "./React/RecoveryRoot"; -import { hash } from "../hash/hash"; +import { commitHash } from "../utils/helpers/commitHash"; import { pushGameReady } from "../Electron"; import initSwc from "@swc/wasm-web"; @@ -17,7 +17,7 @@ export function LoadingScreen(): React.ReactElement { const [show, setShow] = useState(false); const [loaded, setLoaded] = useState(false); - const version = `v${CONSTANTS.VersionString} (${hash()})`; + const version = `v${CONSTANTS.VersionString} (${commitHash()})`; if (process.env.NODE_ENV === "development") { document.title = `[dev] Bitburner ${version}`; } else { diff --git a/src/utils/ErrorHelper.ts b/src/utils/ErrorHelper.ts index 61279a3a10..2a838d5b76 100644 --- a/src/utils/ErrorHelper.ts +++ b/src/utils/ErrorHelper.ts @@ -1,7 +1,7 @@ import React from "react"; -import { Page } from "../ui/Router"; -import { hash } from "../hash/hash"; +import type { Page } from "../ui/Router"; +import { commitHash } from "./helpers/commitHash"; import { CONSTANTS } from "../Constants"; enum GameEnv { @@ -16,7 +16,7 @@ enum Platform { interface GameVersion { version: string; - hash: string; + commitHash: string; toDisplay: () => string; } @@ -54,13 +54,13 @@ export interface IErrorData { export const newIssueUrl = `https://github.com/bitburner-official/bitburner-src/issues/new`; -function getErrorMetadata(error: unknown, errorInfo?: React.ErrorInfo, page?: Page): IErrorMetadata { +export function getErrorMetadata(error: unknown, errorInfo?: React.ErrorInfo, page?: Page): IErrorMetadata { const isElectron = navigator.userAgent.toLowerCase().includes(" electron/"); const env = process.env.NODE_ENV === "development" ? GameEnv.Development : GameEnv.Production; const version: GameVersion = { version: CONSTANTS.VersionString, - hash: hash(), - toDisplay: () => `v${CONSTANTS.VersionString} (${hash()})`, + commitHash: commitHash(), + toDisplay: () => `v${CONSTANTS.VersionString} (${commitHash()})`, }; const features: BrowserFeatures = { userAgent: navigator.userAgent, diff --git a/src/hash/hash.ts b/src/utils/helpers/commitHash.ts similarity index 67% rename from src/hash/hash.ts rename to src/utils/helpers/commitHash.ts index c7d6afdaab..43f521652b 100644 --- a/src/hash/hash.ts +++ b/src/utils/helpers/commitHash.ts @@ -1,4 +1,4 @@ -export function hash(): string { +export function commitHash(): string { try { return __COMMIT_HASH__ ?? "DEV"; } catch { diff --git a/src/utils/helpers/exceptionAlert.ts b/src/utils/helpers/exceptionAlert.ts deleted file mode 100644 index 4ed8c0b216..0000000000 --- a/src/utils/helpers/exceptionAlert.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { dialogBoxCreate } from "../../ui/React/DialogBox"; - -interface IError { - fileName?: string; - lineNumber?: number; -} - -export const isIError = (v: unknown): v is IError => { - if (typeof v !== "object" || v == null) return false; - return Object.hasOwn(v, "fileName") && Object.hasOwn(v, "lineNumber"); -}; - -export function exceptionAlert(e: unknown): void { - console.error(e); - let msg = ""; - let file = "UNKNOWN FILE NAME"; - let line = "UNKNOWN LINE NUMBER"; - if (isIError(e)) { - file = e.fileName ?? file; - line = e.lineNumber?.toString() ?? line; - } else { - msg = String(e); - } - dialogBoxCreate( - "Caught an exception: " + - msg + - "

" + - "Filename: " + - file + - "

" + - "Line Number: " + - line + - "

" + - "This is a bug, please report to game developer with this " + - "message as well as details about how to reproduce the bug.

" + - "If you want to be safe, I suggest refreshing the game WITHOUT saving so that your " + - "save doesn't get corrupted", - true, - ); -} diff --git a/src/utils/helpers/exceptionAlert.tsx b/src/utils/helpers/exceptionAlert.tsx new file mode 100644 index 0000000000..3cd8d9c0c2 --- /dev/null +++ b/src/utils/helpers/exceptionAlert.tsx @@ -0,0 +1,28 @@ +import React from "react"; +import { dialogBoxCreate } from "../../ui/React/DialogBox"; +import Typography from "@mui/material/Typography"; +import { getErrorMetadata } from "../ErrorHelper"; + +export function exceptionAlert(e: unknown): void { + console.error(e); + const errorMetadata = getErrorMetadata(e); + + dialogBoxCreate( + <> + Caught an exception: {String(e)} +
+
+ {e instanceof Error && ( + + Stack: {e.stack?.toString()} + + )} + Commit: {errorMetadata.version.commitHash} +
+ UserAgent: {navigator.userAgent} +
+
+ This is a bug. Please contact developers. + , + ); +}