@swizzyweb/swizzy-web-service
Version:
Web service framework for swizzy dyn serve
20 lines (19 loc) • 592 B
JavaScript
/**
* Asserts the assertion is true, else throws with provided
* message or default message.
* @param props for performing assertion
*/
export function assertOrThrow(props) {
const { args, assertion, errorMessage } = props;
if (!assertion(args)) {
if (typeof errorMessage === "string") {
throw new Error(errorMessage);
}
else if (typeof errorMessage === "function") {
throw new Error(errorMessage(args));
}
else {
throw new Error(`Assertion failed with args: ${JSON.stringify(args)}`);
}
}
}