All files error.ts

100% Statements 131/131
90.62% Branches 29/32
100% Functions 8/8
100% Lines 131/131

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 1731x     1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x   1x     1x   1x 88x 88x     1x 25x 25x     1x 3x 3x 3x 3x   3x 3x 10x 10x 10x 3x 7x 3x 4x   10x     10x 10x   10x 6x 4x   10x 3x 7x   10x 3x 3x 3x   1x 5x 5x 5x 5x 5x   5x 1x   1x 5x 4x 4x 4x 14x 14x 14x   14x 14x 4x 10x   14x 4x 10x   14x 4x 4x 4x   5x 3x 3x   5x 5x     1x 1x 4x 4x 4x 4x 4x 4x 4x     4x 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x   4x 2x 2x 2x 2x 2x 2x 2x 4x 1x     1x 1x 2x 2x     2x 2x 1x     1x 1x 2x 2x 2x 2x 2x 2x 2x     2x 2x 1x  
import path from 'node:path';
 
// Color constants for terminal output
const Colors = {
    RED: '\x1b[31m',
    YELLOW: '\x1b[33m',
    CYAN: '\x1b[36m',
    GRAY: '\x1b[90m',
    RESET: '\x1b[0m',
    BOLD: '\x1b[1m'
};
 
// Check if terminal supports colors
const supportsColor = (): boolean => {
    return (
        !!(process.stdout?.isTTY && process.env.TERM !== 'dumb') ||
        process.env.FORCE_COLOR === '1' ||
        process.env.FORCE_COLOR === 'true'
    );
};
 
// Color formatter utility
const useColors = supportsColor() && process.env.NO_COLOR !== '1';
 
const colorize = (text: string, color: string): string => {
    return useColors ? `${color}${text}${Colors.RESET}` : text;
};
 
// Get relative path from current working directory
const getRelativeFromCwd = (filePath: string): string => {
    return path.relative(process.cwd(), filePath);
};
 
// Formatting functions
export const formatCircularDependency = (
    moduleIds: string[],
    targetModule: string
): string => {
    const fullChain = [...moduleIds, targetModule];
 
    return `${colorize(colorize('Module dependency chain (circular reference found):', Colors.BOLD), Colors.RED)}\n${fullChain
        .map((module, index) => {
            const isLastModule = index === fullChain.length - 1;
            const prefix =
                index === 0
                    ? '┌─ '
                    : index === fullChain.length - 1
                      ? '└─ '
                      : '├─ ';
 
            const displayPath = getRelativeFromCwd(module);
 
            // Check if this module appears elsewhere in the chain (circular dependency)
            const isCircularModule =
                fullChain.filter((m) => m === module).length > 1;
 
            const coloredFile = isCircularModule
                ? colorize(colorize(displayPath, Colors.BOLD), Colors.RED)
                : colorize(displayPath, Colors.CYAN);
 
            const suffix = isLastModule
                ? ` ${colorize('🔄 Creates circular reference', Colors.YELLOW)}`
                : '';
 
            return `${colorize(prefix, Colors.GRAY)}${coloredFile}${suffix}`;
        })
        .join('\n')}`;
};
 
export const formatModuleChain = (
    moduleIds: string[],
    targetModule: string,
    originalError?: Error
): string => {
    let result = '';
 
    if (moduleIds.length === 0) {
        const displayPath = getRelativeFromCwd(targetModule);
 
        result = `${colorize('Failed to load:', Colors.CYAN)} ${colorize(displayPath, Colors.RED)}`;
    } else {
        const chain = [...moduleIds, targetModule];
        result = `${colorize(colorize('Module loading path:', Colors.BOLD), Colors.CYAN)}\n${chain
            .map((module, index) => {
                const indent = '  '.repeat(index);
                const connector = index === 0 ? '' : '└─ ';
                const displayPath = getRelativeFromCwd(module);
 
                const isFailedFile = index === chain.length - 1;
                const coloredFile = isFailedFile
                    ? colorize(colorize(displayPath, Colors.BOLD), Colors.RED)
                    : colorize(displayPath, Colors.CYAN);
 
                const status = isFailedFile
                    ? ` ${colorize(colorize('❌ Loading failed', Colors.BOLD), Colors.RED)}`
                    : '';
 
                return `${colorize(indent + connector, Colors.GRAY)}${coloredFile}${status}`;
            })
            .join('\n')}`;
    }
 
    if (originalError) {
        result += `\n\n${colorize('Error details:', Colors.YELLOW)} ${originalError.message}`;
    }
 
    return result;
};
 
// Base module loading error class
export class ModuleLoadingError extends Error {
    constructor(
        message: string,
        public moduleIds: string[],
        public targetModule: string,
        public originalError?: Error
    ) {
        super(message);
        this.name = 'ModuleLoadingError';
 
        // Hide auxiliary properties from enumeration to avoid cluttering error display
        Object.defineProperty(this, 'moduleIds', {
            value: moduleIds,
            writable: false,
            enumerable: false,
            configurable: true
        });
 
        Object.defineProperty(this, 'targetModule', {
            value: targetModule,
            writable: false,
            enumerable: false,
            configurable: true
        });
 
        if (originalError) {
            Object.defineProperty(this, 'originalError', {
                value: originalError,
                writable: false,
                enumerable: false,
                configurable: true
            });
        }
    }
}
 
// Circular dependency error class
export class CircularDependencyError extends ModuleLoadingError {
    constructor(message: string, moduleIds: string[], targetModule: string) {
        super(message, moduleIds, targetModule);
        this.name = 'CircularDependencyError';
 
        // Custom stack for clean error display
        this.stack = `${this.name}: ${message}\n\n${formatCircularDependency(moduleIds, targetModule)}`;
    }
}
 
// File read error class
export class FileReadError extends ModuleLoadingError {
    constructor(
        message: string,
        moduleIds: string[],
        targetModule: string,
        originalError?: Error
    ) {
        super(message, moduleIds, targetModule, originalError);
        this.name = 'FileReadError';
 
        // Custom stack for clean error display
        this.stack = `${this.name}: ${message}\n\n${formatModuleChain(moduleIds, targetModule, originalError)}`;
    }
}