MISC: Improve exception alert (#1709)

This commit is contained in:
catloversg
2024-10-22 10:36:29 +07:00
committed by GitHub
parent 8d1cfb2d97
commit bc51733fbe
10 changed files with 47 additions and 59 deletions
+6 -6
View File
@@ -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,
+7
View File
@@ -0,0 +1,7 @@
export function commitHash(): string {
try {
return __COMMIT_HASH__ ?? "DEV";
} catch {
return "DEV";
}
}
-40
View File
@@ -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 +
"<br><br>" +
"Filename: " +
file +
"<br><br>" +
"Line Number: " +
line +
"<br><br>" +
"This is a bug, please report to game developer with this " +
"message as well as details about how to reproduce the bug.<br><br>" +
"If you want to be safe, I suggest refreshing the game WITHOUT saving so that your " +
"save doesn't get corrupted",
true,
);
}
+28
View File
@@ -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)}
<br />
<br />
{e instanceof Error && (
<Typography component="div" style={{ whiteSpace: "pre-wrap" }}>
Stack: {e.stack?.toString()}
</Typography>
)}
Commit: {errorMetadata.version.commitHash}
<br />
UserAgent: {navigator.userAgent}
<br />
<br />
This is a bug. Please contact developers.
</>,
);
}