@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.
35 lines (30 loc) • 935 B
text/typescript
import { useErrorAlchemy } from "../../../crafting/useErrorAlchemy/useErrorAlchemy";
const { craftMysticError } = useErrorAlchemy(
"ErrorAlchemy",
"assertTruthy-function"
);
/**
* Type for AssertTruthyFailedError.
*/
export type AssertTruthyFailedError = InstanceType<
ReturnType<typeof craftMysticError>
>;
export const AssertTruthyFailedError = craftMysticError({
name: "AssertTruthyFailedError",
errorCode: "DATA_INTEGRITY_VIOLATION",
severity: "critical",
cause: "value was falsy",
});
/**
* Asserts that a given value is truthy.
* @param {T} value - The value to be checked for truthiness.
* @throws {AssertTruthyFailedError} If the value is falsy.
*/
export function assertTruthy<T>(value: T): asserts value is NonNullable<T> {
if (!value) {
throw new AssertTruthyFailedError({
message: `Assertion failed. Expected a truthy value.`,
payload: { value, type: typeof value },
});
}
}