Catch global errors and rejected promises and display them in an alert box. (#351)

* Catch errors and rejected promises.

* Clean up
pull/398/head
James Lin 2024-05-02 05:44:10 -07:00 committed by GitHub
parent 10506f9cf5
commit 0185dd639e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 1 deletions

View File

@ -9,6 +9,22 @@ import BBCodeText from 'phaser3-rex-plugins/plugins/bbcodetext';
import TransitionImagePackPlugin from 'phaser3-rex-plugins/templates/transitionimagepack/transitionimagepack-plugin.js'; import TransitionImagePackPlugin from 'phaser3-rex-plugins/templates/transitionimagepack/transitionimagepack-plugin.js';
import { LoadingScene } from './loading-scene'; 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 = { const config: Phaser.Types.Core.GameConfig = {
type: Phaser.WEBGL, type: Phaser.WEBGL,
parent: 'app', parent: 'app',

View File

@ -331,6 +331,8 @@ export class TitlePhase extends Phase {
this.scene.sessionPlayTime = 0; this.scene.sessionPlayTime = 0;
this.end(); this.end();
}); });
}).catch(err => {
console.error("Failed to load daily run:\n", err);
}); });
}); });
} }

View File

@ -167,7 +167,7 @@ export class DailyRunScoreboard extends Phaser.GameObjects.Container {
} else } else
this.loadingLabel.setText(i18next.t('menu:noRankings')); this.loadingLabel.setText(i18next.t('menu:noRankings'));
}); });
}); }).catch(err => { console.error("Failed to load daily rankings:\n", err) });
} }
} }

View File

@ -65,6 +65,9 @@ export default class TitleUiHandler extends OptionSelectUiHandler {
this.playerCountLabel.setText(`${stats.playerCount} ${i18next.t("menu:playersOnline")}`); this.playerCountLabel.setText(`${stats.playerCount} ${i18next.t("menu:playersOnline")}`);
if (this.splashMessage === battleCountSplashMessage) if (this.splashMessage === battleCountSplashMessage)
this.splashMessageText.setText(battleCountSplashMessage.replace('{COUNT}', stats.battleCount.toLocaleString('en-US'))); this.splashMessageText.setText(battleCountSplashMessage.replace('{COUNT}', stats.battleCount.toLocaleString('en-US')));
})
.catch(err => {
console.error("Failed to fetch title stats:\n", err);
}); });
} }