ds4/src/utils.ts

25 lines
977 B
TypeScript
Raw Normal View History

2021-07-23 12:33:52 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
import { getGameSafe } from "./helpers";
2021-07-23 00:43:15 +02:00
2021-07-23 12:48:11 +02:00
/**
* Tests if the given `value` is truthy.
*
* If it is not truthy, an {@link Error} is thrown, which depends on the given `message` parameter:
* - If `message` is a string`, it is used to construct a new {@link Error} which then is thrown.
* - If `message` is an instance of {@link Error}, it is thrown.
* - If `message` is `undefined`, an {@link Error} with a default message is thrown.
*/
2021-07-23 00:43:15 +02:00
export function enforce(value: unknown, message?: string | Error): asserts value {
if (!value) {
if (!message) {
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).";
2021-07-23 00:43:15 +02:00
}
throw message instanceof Error ? message : new Error(message);
}
}