/** * @fileoverview CON-04 Runtime Validation Errors * @description Standardized error classes for validation failures * @version 0.18.4 */ /** * Base validation error class for runtime validation failures * Extends Error to maintain compatibility with existing error handling */ export class ValidationError extends Error { public readonly details?: Record; public readonly timestamp: string; public readonly code: string; constructor(message: string, code: string = 'VALIDATION_ERROR', cause?: Error | unknown, details?: Record) { super(message); this.name = 'ValidationError'; this.code = code; this.details = details; this.timestamp = new Date().toISOString(); // Maintain proper stack trace for where our error was thrown (only available on V8) if (Error.captureStackTrace) { Error.captureStackTrace(this, ValidationError); } // Handle cause if provided (Node.js 16+ and browsers with Error.cause support) if (cause instanceof Error) { this.cause = cause; } } /** * Convert to JSON for logging/serialization */ toJSON() { return { name: this.name, code: this.code, message: this.message, details: this.details, timestamp: this.timestamp, stack: this.stack, cause: this.cause instanceof Error ? { name: this.cause.name, message: this.cause.message, stack: this.cause.stack } : this.cause }; } } /** * Error thrown when payload validation fails */ export class PayloadValidationError extends ValidationError { constructor(message: string, code?: string, cause?: Error | unknown, details?: Record) { super(message, code || 'PAYLOAD_VALIDATION_ERROR', cause, details); this.name = 'PayloadValidationError'; } } /** * Error thrown when envelope validation fails */ export class EnvelopeValidationError extends ValidationError { constructor(message: string, code?: string, cause?: Error | unknown, details?: Record) { super(message, code || 'ENVELOPE_VALIDATION_ERROR', cause, details); this.name = 'EnvelopeValidationError'; } } /** * Error thrown when type guard validation fails */ export class TypeGuardError extends ValidationError { constructor(message: string, code?: string, cause?: Error | unknown, details?: Record) { super(message, code || 'TYPE_GUARD_ERROR', cause, details); this.name = 'TypeGuardError'; } } /** * Error thrown when route validation fails */ export class RouteValidationError extends ValidationError { constructor(message: string, code?: string, cause?: Error | unknown, details?: Record) { super(message, code || 'ROUTE_VALIDATION_ERROR', cause, details); this.name = 'RouteValidationError'; } }