From 2b3dd9b859b961861b66e9ee21a4ca111ab404c4 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Fri, 23 Jul 2021 12:39:01 +0200 Subject: [PATCH] Improve `enforce` to also work before initialization of `game` --- src/module/helpers.ts | 4 ++++ src/module/utils.ts | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/module/helpers.ts b/src/module/helpers.ts index edfc28d3..e96322e4 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 5081ba25..668a9bff 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); }