UNPKG

behemoth-cli

Version:

🌍 BEHEMOTH CLIv3.760.4 - Level 50+ POST-SINGULARITY Intelligence Trading AI

120 lines â€ĸ 4.88 kB
/** * Standardized Error Handling Utility * Provides consistent error logging and handling patterns */ export var ErrorLevel; (function (ErrorLevel) { ErrorLevel["DEBUG"] = "debug"; ErrorLevel["INFO"] = "info"; ErrorLevel["WARN"] = "warn"; ErrorLevel["ERROR"] = "error"; ErrorLevel["FATAL"] = "fatal"; })(ErrorLevel || (ErrorLevel = {})); export class ErrorHandler { static isDevelopment = process.env.NODE_ENV === 'development'; static isDebugEnabled = process.env.DEBUG === 'true'; /** * Log an error with consistent formatting and context */ static log(level, message, error, context) { const timestamp = new Date().toISOString(); const prefix = context?.component ? `[${context.component}]` : ''; const operation = context?.operation ? ` ${context.operation}:` : ''; const formattedMessage = `${prefix}${operation} ${message}`; // Only log debug messages in development or when debug is enabled if (level === ErrorLevel.DEBUG && !this.isDevelopment && !this.isDebugEnabled) { return; } switch (level) { case ErrorLevel.DEBUG: if (this.isDevelopment || this.isDebugEnabled) { console.debug(`🐛 ${timestamp} ${formattedMessage}`, error); } break; case ErrorLevel.INFO: console.info(`â„šī¸ ${timestamp} ${formattedMessage}`); break; case ErrorLevel.WARN: console.warn(`âš ī¸ ${timestamp} ${formattedMessage}`, error || ''); break; case ErrorLevel.ERROR: console.error(`❌ ${timestamp} ${formattedMessage}`, error || ''); break; case ErrorLevel.FATAL: console.error(`💀 ${timestamp} FATAL: ${formattedMessage}`, error || ''); break; } // Log additional context in development if (context?.metadata && this.isDevelopment) { console.debug('Context:', context.metadata); } } /** * Create a structured error result for consistent error responses */ static createErrorResult(message, error, code) { return { success: false, error: error instanceof Error ? `${message}: ${error.message}` : message, code, timestamp: new Date().toISOString() }; } /** * Wrap async operations with consistent error handling */ static async withErrorHandling(operation, context) { try { const data = await operation(); return { success: true, data }; } catch (error) { this.log(ErrorLevel.ERROR, context.errorMessage, error, context); return this.createErrorResult(context.errorMessage, error); } } /** * Safe error message extraction from unknown error types */ static extractErrorMessage(error) { if (error instanceof Error) { return error.message; } if (typeof error === 'string') { return error; } if (error && typeof error === 'object' && 'message' in error) { return String(error.message); } return 'Unknown error occurred'; } /** * Validate and sanitize error messages to prevent information leakage */ static sanitizeErrorMessage(message, isProduction = process.env.NODE_ENV === 'production') { if (!isProduction) { return message; // Full error details in development } // Remove sensitive patterns in production const sensitivePatterns = [ /api[_-]?key[:\s=]+[^\s]+/gi, /password[:\s=]+[^\s]+/gi, /token[:\s=]+[^\s]+/gi, /secret[:\s=]+[^\s]+/gi, /\/home\/[^\s]+/gi, // Remove file paths /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/gi // Remove email addresses ]; let sanitized = message; sensitivePatterns.forEach(pattern => { sanitized = sanitized.replace(pattern, '[REDACTED]'); }); return sanitized; } } // Convenience functions for common error levels export const logDebug = (message, error, context) => ErrorHandler.log(ErrorLevel.DEBUG, message, error, context); export const logInfo = (message, context) => ErrorHandler.log(ErrorLevel.INFO, message, undefined, context); export const logWarn = (message, error, context) => ErrorHandler.log(ErrorLevel.WARN, message, error, context); export const logError = (message, error, context) => ErrorHandler.log(ErrorLevel.ERROR, message, error, context); export const logFatal = (message, error, context) => ErrorHandler.log(ErrorLevel.FATAL, message, error, context); //# sourceMappingURL=error-handler.js.map