UNPKG

gather-ts

Version:

A powerful code analysis and packaging tool designed for creating AI-friendly code representations for javascript and typescript projects.

148 lines 5.97 kB
"use strict"; // src/errors/handlers/ErrorHandler.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorHandler = void 0; const services_1 = require("@/types/services"); const exceptions_1 = require("../exceptions"); class ErrorHandler extends services_1.BaseService { constructor(deps, options = {}) { super(); this.deps = deps; this.processingStrategies = []; this.debug = options.debug || false; this.logToConsole = options.logToConsole ?? true; this.logToFile = options.logToFile || false; this.logFilePath = options.logFilePath; this.rethrow = options.rethrow || false; if (this.logToConsole) { this.registerStrategy(this.createConsoleStrategy()); } } async initialize() { await super.initialize(); this.logDebug("Initializing ErrorHandler"); try { if (this.logToFile && this.logFilePath) { this.logDebug(`Setting up file logging at ${this.logFilePath}`); const logDir = this.deps.fileSystem.getDirName(this.logFilePath); if (!this.deps.fileSystem.exists(logDir)) { await this.deps.fileSystem.createDirectory(logDir, true); } this.registerStrategy(this.createFileStrategy()); } this.logDebug("ErrorHandler initialization complete"); } catch (error) { this.deps.logger.error(`Failed to initialize ErrorHandler: ${error instanceof Error ? error.message : String(error)}`); throw error; } } cleanup() { this.logDebug("Cleaning up ErrorHandler"); this.processingStrategies.length = 0; super.cleanup(); } logDebug(message) { if (this.debug) { this.deps.logger.debug(message); } } registerStrategy(strategy) { this.logDebug("Registering new error processing strategy"); this.processingStrategies.push(strategy); } handle(error, options = {}) { this.logDebug(`Handling error: ${error.message}`); const normalizedError = this.deps.errorUtils.normalizeError(error); const context = this.deps.errorUtils.extractErrorContext(normalizedError); let processedByStrategy = false; for (const strategy of this.processingStrategies) { if (strategy.shouldHandle(normalizedError)) { processedByStrategy = true; this.logDebug("Processing error through strategy"); strategy.handle(normalizedError, context); } } if (!processedByStrategy) { this.logDebug("No strategy handled the error, using default console logging"); this.deps.logger.error(normalizedError.message); } if (options.rethrow || this.rethrow) { this.logDebug("Rethrowing error as configured"); throw normalizedError; } } handleBatch(errors) { this.logDebug(`Processing batch of ${errors.length} errors`); if (errors.length === 0) { return; } const aggregatedError = this.deps.errorUtils.aggregateErrors(errors); this.handle(aggregatedError); } async createErrorBoundary(fn) { try { await fn(); } catch (error) { this.logDebug("Error caught in error boundary"); this.handle(error instanceof Error ? error : new Error(String(error))); return Promise.reject(error); } } createConsoleStrategy() { return { shouldHandle: () => true, handle: (error, context) => { const classification = this.deps.errorUtils.classifyError(error); switch (classification.severity) { case "error": this.deps.logger.error(classification.message); if (classification.details) { if (this.debug) { this.deps.logger.debug(`Error details: ${JSON.stringify(classification.details)}`); } } break; case "warning": this.deps.logger.warn(classification.message); break; case "info": this.deps.logger.info(classification.message); break; } if (this.debug && classification.stackTrace) { this.logDebug("Stack trace:" + classification.stackTrace); } }, }; } createFileStrategy() { if (!this.logFilePath) { throw new Error("Log file path not configured"); } return { shouldHandle: (error) => error instanceof exceptions_1.GatherTSError, handle: (error, context) => { const timestamp = new Date().toISOString(); const classification = this.deps.errorUtils.classifyError(error); const logEntry = { timestamp, level: classification.severity, type: classification.type, message: classification.message, details: classification.details, context, }; try { this.deps.fileSystem.writeFileSync(this.logFilePath, JSON.stringify(logEntry) + "\n", { flag: "a" }); } catch (writeError) { this.deps.logger.error(`Failed to write to error log: ${writeError instanceof Error ? writeError.message : String(writeError)}`); } }, }; } } exports.ErrorHandler = ErrorHandler; //# sourceMappingURL=ErrorHandler.js.map