From 0185dd639e0238b60a248d4f0f51dc89c245bebe Mon Sep 17 00:00:00 2001 From: James Lin Date: Thu, 2 May 2024 05:44:10 -0700 Subject: [PATCH] Catch global errors and rejected promises and display them in an alert box. (#351) * Catch errors and rejected promises. * Clean up --- src/main.ts | 16 ++++++++++++++++ src/phases.ts | 2 ++ src/ui/daily-run-scoreboard.ts | 2 +- src/ui/title-ui-handler.ts | 3 +++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 6a00693fc..4b181f581 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,22 @@ import BBCodeText from 'phaser3-rex-plugins/plugins/bbcodetext'; import TransitionImagePackPlugin from 'phaser3-rex-plugins/templates/transitionimagepack/transitionimagepack-plugin.js'; import { LoadingScene } from './loading-scene'; + +// Catch global errors and display them in an alert so users can report the issue. +window.onerror = function (message, source, lineno, colno, error) { + console.error(error); + let errorString = `Received unhandled error. Open browser console and click OK to see details.\nError: ${message}\nSource: ${source}\nLine: ${lineno}\nColumn: ${colno}\nStack: ${error.stack}`; + alert(errorString); + // Avoids logging the error a second time. + return true; +}; + +// Catch global promise rejections and display them in an alert so users can report the issue. +window.addEventListener('unhandledrejection', (event) => { + let errorString = `Received unhandled promise rejection. Open browser console and click OK to see details.\nReason: ${event.reason}`; + alert(errorString); + }); + const config: Phaser.Types.Core.GameConfig = { type: Phaser.WEBGL, parent: 'app', diff --git a/src/phases.ts b/src/phases.ts index 72f0e10cc..2ffa25aef 100644 --- a/src/phases.ts +++ b/src/phases.ts @@ -331,6 +331,8 @@ export class TitlePhase extends Phase { this.scene.sessionPlayTime = 0; this.end(); }); + }).catch(err => { + console.error("Failed to load daily run:\n", err); }); }); } diff --git a/src/ui/daily-run-scoreboard.ts b/src/ui/daily-run-scoreboard.ts index 8f7ca0bf1..8b258b3a7 100644 --- a/src/ui/daily-run-scoreboard.ts +++ b/src/ui/daily-run-scoreboard.ts @@ -167,7 +167,7 @@ export class DailyRunScoreboard extends Phaser.GameObjects.Container { } else this.loadingLabel.setText(i18next.t('menu:noRankings')); }); - }); + }).catch(err => { console.error("Failed to load daily rankings:\n", err) }); } } diff --git a/src/ui/title-ui-handler.ts b/src/ui/title-ui-handler.ts index c430764e4..6f873c6b0 100644 --- a/src/ui/title-ui-handler.ts +++ b/src/ui/title-ui-handler.ts @@ -65,6 +65,9 @@ export default class TitleUiHandler extends OptionSelectUiHandler { this.playerCountLabel.setText(`${stats.playerCount} ${i18next.t("menu:playersOnline")}`); if (this.splashMessage === battleCountSplashMessage) this.splashMessageText.setText(battleCountSplashMessage.replace('{COUNT}', stats.battleCount.toLocaleString('en-US'))); + }) + .catch(err => { + console.error("Failed to fetch title stats:\n", err); }); }