UNPKG

gather-ts

Version:

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

136 lines 4.39 kB
"use strict"; // src/errors/utils/ErrorUtils.ts Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorUtils = void 0; const exceptions_1 = require("../exceptions"); const services_1 = require("@/types/services"); class ErrorUtils extends services_1.BaseService { constructor(deps, options = {}) { super(); this.deps = deps; this.debug = options.debug || false; } async initialize() { await super.initialize(); this.logDebug("ErrorUtils service initialized"); } cleanup() { this.logDebug("ErrorUtils service cleanup"); super.cleanup(); } logDebug(message) { if (this.debug) { this.deps.logger.debug(message); } } isGatherTSError(error) { this.checkInitialized(); return error instanceof exceptions_1.GatherTSError; } wrapError(error, context) { if (error instanceof exceptions_1.GatherTSError) { return error; } const message = error instanceof Error ? error.message : String(error); const wrapped = new Error(`${context}: ${message}`); if (error instanceof Error) { wrapped.stack = error.stack; } return wrapped; } formatErrorDetails(details) { return Object.entries(details) .filter(([_, value]) => value !== undefined) .map(([key, value]) => { if (Array.isArray(value)) { return `${key}: [${value.join(", ")}]`; } if (value instanceof Error) { return `${key}: ${value.message}`; } if (typeof value === "object" && value !== null) { return `${key}: ${JSON.stringify(value)}`; } return `${key}: ${value}`; }) .join(", "); } aggregateErrors(errors) { if (errors.length === 0) { return new Error("No errors to aggregate"); } if (errors.length === 1) { return errors[0]; } const messages = errors.map((err, index) => { const prefix = errors.length > 1 ? `${index + 1}. ` : ""; return prefix + (err instanceof Error ? err.message : String(err)); }); const err = new Error(`Multiple errors occurred:\n${messages.join("\n")}`); err.name = "AggregateError"; return err; } classifyError(error) { const baseClassification = { type: "UnknownError", severity: "error", message: "", details: undefined, stackTrace: undefined, }; if (error instanceof exceptions_1.GatherTSError) { return { ...baseClassification, type: error.name, message: error.message, details: error.details, stackTrace: error.stack, }; } if (error instanceof Error) { return { ...baseClassification, type: error.name, message: error.message, stackTrace: error.stack, }; } return { ...baseClassification, message: String(error), }; } transformError(error, transformations) { let transformed = error instanceof Error ? error : new Error(String(error)); for (const transformation of transformations) { if (transformation.condition(transformed)) { transformed = transformation.transform(transformed); } } return transformed; } extractErrorContext(error) { const context = { name: error.name, message: error.message, }; if (error instanceof exceptions_1.GatherTSError && error.details) { context.details = error.details; } if (this.debug && error.stack) { context.stack = error.stack; } return context; } normalizeError(error) { if (error instanceof Error) { return error; } if (error instanceof Object) { return new Error(JSON.stringify(error)); } return new Error(String(error)); } } exports.ErrorUtils = ErrorUtils; //# sourceMappingURL=ErrorUtils.js.map