@ocubist/error-alchemy
Version:
A powerful Node.js error-handling-framework with custom error types for easy debugging, enhanced error management, strong zod-support and useful quality-of-life-functionality for asserting and validating values.
37 lines (31 loc) • 1 kB
text/typescript
import { useErrorAlchemy } from "../../../crafting/useErrorAlchemy/useErrorAlchemy";
const { craftMysticError } = useErrorAlchemy(
"ErrorAlchemy",
"assertFalsy-function"
);
/**
* Type for AssertFalsyFailedError.
*/
export type AssertFalsyFailedError = InstanceType<
ReturnType<typeof craftMysticError>
>;
export const AssertFalsyFailedError = craftMysticError({
name: "AssertFalsyFailedError",
errorCode: "DATA_INTEGRITY_VIOLATION",
severity: "critical",
cause: "value was truthy",
});
export type Falsy<T> = T extends false | "" | 0 | null | undefined ? T : never;
/**
* Asserts that a given value is falsy.
* @param {T} value - The value to be checked for falsiness.
* @throws {AssertFalsyFailedError} If the value is truthy.
*/
export function assertFalsy<T>(value: T): asserts value is Falsy<T> {
if (value) {
throw new AssertFalsyFailedError({
message: `Assertion failed. Expected a falsy value.`,
payload: { value, type: typeof value },
});
}
}