All files / sdk/utils/errors/src composioError.ts

72.41% Statements 21/29
33.33% Branches 11/33
25% Functions 1/4
72.41% Lines 21/29

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 11516x 16x 16x         16x                     5x                                       5x     5x   5x 5x 5x 5x 5x 5x 5x   5x     5x 1x 1x             1x         5x                         5x                                                                  
import { v4 as uuidv4 } from 'uuid';
import { getLogLevel } from '../../../../utils/logger';
import { logError } from '..';
 
/**
 * Custom error class for Composio that provides rich error details, tracking, and improved debugging
 */
export class ComposioError extends Error {
    // time at which the error occurred
    public readonly timestamp: string;
 
    // unique identifier for the error
    private readonly errorId: string;
 
    // error code
    private readonly errCode: string;
 
    // additional metadata about the error
    private readonly metadata?: Record<string, any> = {};
 
    // description of the error
    private readonly description?: string;
 
    // possible fix for the error
    private readonly possibleFix?: string;
 
    // original error object
    private readonly _originalError?: any;
 
    constructor(
        errCode: string,
        message: string,
        description?: string,
        possibleFix?: string,
        metadata?: Record<string, any>,
        originalError?: any,
    ) {
        // Ensure message is never empty
        super(message || 'An unknown error occurred');
 
        // Ensure proper prototype chain for instanceof checks
        Object.setPrototypeOf(this, new.target.prototype);
 
        this.name = 'ComposioError';
        this.errCode = errCode;
        this.description = description;
        this.possibleFix = possibleFix;
        this.timestamp = new Date().toISOString();
        this.metadata = metadata;
        this.errorId = uuidv4();
 
        let originalErrorString: string = '';
 
        // Only print original error if COMPOSIO_LOGGING_LEVEL is debug
        if (originalError) {
            try {
                originalErrorString = typeof originalError === 'object'
                    ? JSON.parse(JSON.stringify(originalError))
                    : originalError;
            } catch (e) {
                originalErrorString = String(originalError);
            }
 
            Iif (getLogLevel() === 'debug') {
                this._originalError = originalErrorString;
            }
        }
 
        logError({
            error_id: this.errorId,
            error_code: this.errCode,
            original_error: originalErrorString,
            description: this.description || '',
            metadata: this.metadata || {},
            message: this.message,
            possible_fix: this.possibleFix || '',
            current_stack: this.stack?.split('\n') || []
        });
 
        
        // Capture stack trace, excluding constructor call
        Error.captureStackTrace(this, this.constructor);
    }
 
    get originalError(): any {
        return this._originalError;
    }
 
 
    /**
     * Returns a complete object representation for logging/serialization
     * Includes all error details and metadata
     */
    toJSON(): Record<string, any> {
        const errorObj = {
            name: this.name,
            errorId: this.errorId,
            code: this.errCode,
            message: this.message,
            description: this.description,
            possibleFix: this.possibleFix,
            timestamp: this.timestamp,
            stack: this.stack?.split('\n'),
            originalStack: this.originalError?.stack?.split('\n'),
        };
 
        // Remove undefined/null properties
        return Object.entries(errorObj).reduce((acc, [key, value]) => {
            Iif (value !== undefined && value !== null) {
                acc[key] = value;
            }
            return acc;
        }, {} as Record<string, any>);
    }
}