/** * DError - Extended Error class with error codes and metadata * * Provides a custom error class that extends Error with: * - Error codes for categorization * - Error metadata for additional context * - Backward compatibility with standard Error */ export class DError extends Error { public code: string; public metadata?: Record; public timestamp: number; constructor( message: string, code: string = "UNKNOWN_ERROR", metadata?: Record ) { super(message); this.name = "DError"; this.code = code; this.metadata = metadata; this.timestamp = Date.now(); // Maintain proper prototype chain Object.setPrototypeOf(this, DError.prototype); } /** * Convert error to JSON for logging/serialization */ toJSON() { return { name: this.name, message: this.message, code: this.code, metadata: this.metadata, timestamp: this.timestamp, stack: this.stack, }; } /** * Convert error to string representation */ toString(): string { return `[${this.code}] ${this.message}`; } } export namespace dassert { export function verifyOrThrow( cond: boolean, msg: string, code: string = "VERIFICATION_FAILED" ) { if (!cond) { throw new DError(msg, code); } } export function verifyNotNullAndEmpty( data: any, msg: string, code: string = "NULL_OR_EMPTY_ERROR" ) { if ( data == null || data === undefined || (typeof data === "object" && Object.keys(data).length === 0) ) { throw new DError(msg, code, { data }); } // empty string if (typeof data === "string" && data.trim().length === 0) { throw new DError(msg, code, { data }); } // empty array if (Array.isArray(data) && data.length === 0) { throw new DError(msg, code, { data }); } } export function verifyOrCrash( cond: boolean, msg: string = "Verify and crash called", code: string = "CRASH_ERROR" ) { if (!cond) { throw new DError(msg, code); } } export function assertNotEmpty( data: string, msg: string = "Data is empty which I am not expecting", code: string = "EMPTY_DATA_ERROR" ) { if (data == null || data === undefined || data.length === 0) { throw new DError(msg, code, { data }); } } export function getOrThrow( cond: boolean, msg: string, code: string = "GET_OR_THROW_ERROR" ) { if (!cond) { throw new DError(msg, code); } else { return cond; } } export function verifyNotNullAndUndef( data: any, msg: string, code: string = "NULL_OR_UNDEFINED_ERROR" ) { if (data == null || data === undefined) { throw new DError(msg, code, { data }); } } export function verifyObject( data: any, msg: string, code: string = "NOT_OBJECT_ERROR" ) { if (Array.isArray(data)) { throw new DError(msg, code, { data, type: "array" }); } if (typeof data !== "object" || data === null) { throw new DError(msg, code, { data, type: typeof data }); } } export function verifyArray( data: any, msg: string, code: string = "NOT_ARRAY_ERROR" ) { if (!Array.isArray(data)) { throw new DError(msg, code, { data, type: typeof data }); } } export function verifyString( data: any, msg: string, code: string = "NOT_STRING_ERROR" ) { if (typeof data !== "string") { throw new DError(msg, code, { data, type: typeof data }); } } export function verifyValidItem( item: string, arr: string[], code: string = "INVALID_ITEM_ERROR" ) { if (!arr.includes(item)) { throw new DError( `Invalid value <${item}>, The input value must belong to ${JSON.stringify( arr )}`, code, { item, validItems: arr } ); } } }