Skip to content

Commit

Permalink
GANG: Show error popup when there are errors (bitburner-official#1763)
Browse files Browse the repository at this point in the history
* GANG: Show error popup when there are errors

* Only show error once when it's in a hot code path
  • Loading branch information
catloversg authored Nov 11, 2024
1 parent 72a63b1 commit 5d26f4f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
5 changes: 1 addition & 4 deletions src/Gang/Gang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ export class Gang {

/** Main process function called by the engine loop every game cycle */
process(numCycles = 1): void {
if (isNaN(numCycles)) {
console.error(`NaN passed into Gang.process(): ${numCycles}`);
}
this.storedCycles += numCycles;
if (this.storedCycles < GangConstants.minCyclesToProcess) return;

Expand All @@ -112,7 +109,7 @@ export class Gang {
this.processTerritoryAndPowerGains(cycles);
this.storedCycles -= cycles;
} catch (e: unknown) {
console.error("Exception caught when processing Gang", e);
exceptionAlert(e, true);
}

// Handle "nextUpdate" resolver after this update
Expand Down
7 changes: 5 additions & 2 deletions src/engine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ const Engine: {

decrementAllCounters: function (numCycles = 1) {
for (const [counterName, counter] of Object.entries(Engine.Counters)) {
if (counter === undefined) throw new Error("counter should not be undefined");
if (counter === undefined) {
exceptionAlert(new Error(`counter value is undefined. counterName: ${counterName}.`), true);
continue;
}
Engine.Counters[counterName] = counter - numCycles;
}
},
Expand Down Expand Up @@ -207,7 +210,7 @@ const Engine: {
try {
Player.bladeburner.process();
} catch (e) {
exceptionAlert(e);
exceptionAlert(e, true);
}
}
Engine.Counters.mechanicProcess = 5;
Expand Down
36 changes: 30 additions & 6 deletions src/utils/helpers/exceptionAlert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,43 @@ import React from "react";
import { dialogBoxCreate } from "../../ui/React/DialogBox";
import Typography from "@mui/material/Typography";
import { getErrorMetadata } from "../ErrorHelper";
import { cyrb53 } from "../StringHelperFunctions";

export function exceptionAlert(e: unknown): void {
console.error(e);
const errorMetadata = getErrorMetadata(e);
const errorSet = new Set<string>();

/**
* Show the error in a popup:
* - Indicate that this is a bug and should be reported to developers.
* - Automatically include debug information (e.g., stack trace, commit id, user agent).
*
* @param error Error
* @param showOnlyOnce Set to true if you want to show the error only once, even when it happens many times. Default: false.
* @returns
*/
export function exceptionAlert(error: unknown, showOnlyOnce = false): void {
console.error(error);
const errorAsString = String(error);
const errorStackTrace = error instanceof Error ? error.stack : undefined;
if (showOnlyOnce) {
// Calculate the "id" of the error.
const errorId = cyrb53(errorAsString + errorStackTrace);
// Check if we showed it
if (errorSet.has(errorId)) {
return;
} else {
errorSet.add(errorId);
}
}
const errorMetadata = getErrorMetadata(error);

dialogBoxCreate(
<>
Caught an exception: {String(e)}
Caught an exception: {errorAsString}
<br />
<br />
{e instanceof Error && (
{errorStackTrace && (
<Typography component="div" style={{ whiteSpace: "pre-wrap" }}>
Stack: {e.stack?.toString()}
Stack: {errorStackTrace}
</Typography>
)}
Commit: {errorMetadata.version.commitHash}
Expand Down

0 comments on commit 5d26f4f

Please sign in to comment.