Improve enforce to also work before initialization of game

This commit is contained in:
Johannes Loher 2021-07-23 12:39:01 +02:00
parent 20226e30b1
commit 2b3dd9b859
2 changed files with 8 additions and 2 deletions

View file

@ -15,3 +15,7 @@ export function getGame(): Game {
} }
return game; return game;
} }
export function getGameSafe(): Game | undefined {
return game instanceof Game ? game : undefined;
}

View file

@ -2,12 +2,14 @@
// //
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
import { getGame } from "./helpers"; import { getGameSafe } from "./helpers";
export function enforce(value: unknown, message?: string | Error): asserts value { export function enforce(value: unknown, message?: string | Error): asserts value {
if (!value) { if (!value) {
if (!message) { if (!message) {
message = getGame().i18n.localize("DS4.ErrorUnexpectedError"); message =
getGameSafe()?.i18n.localize("DS4.ErrorUnexpectedError") ??
"There was an unexpected error in the Dungeonslayers 4 system. For more details, please take a look at the console (F12).";
} }
throw message instanceof Error ? message : new Error(message); throw message instanceof Error ? message : new Error(message);
} }