Improve enforce
to also work before initialization of game
This commit is contained in:
parent
20226e30b1
commit
2b3dd9b859
2 changed files with 8 additions and 2 deletions
|
@ -15,3 +15,7 @@ export function getGame(): Game {
|
||||||
}
|
}
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getGameSafe(): Game | undefined {
|
||||||
|
return game instanceof Game ? game : undefined;
|
||||||
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue