diff --git a/src/module/helpers.ts b/src/module/helpers.ts index edfc28d..e96322e 100644 --- a/src/module/helpers.ts +++ b/src/module/helpers.ts @@ -15,3 +15,7 @@ export function getGame(): Game { } return game; } + +export function getGameSafe(): Game | undefined { + return game instanceof Game ? game : undefined; +} diff --git a/src/module/utils.ts b/src/module/utils.ts index 5081ba2..668a9bf 100644 --- a/src/module/utils.ts +++ b/src/module/utils.ts @@ -2,12 +2,14 @@ // // SPDX-License-Identifier: MIT -import { getGame } from "./helpers"; +import { getGameSafe } from "./helpers"; export function enforce(value: unknown, message?: string | Error): asserts value { if (!value) { 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); }