/** * Base Orbis Error * * Base error class for all SDK errors * * @module errors/OrbisError */ /** * Error codes for Orbis SDK errors */ export enum OrbisErrorCode { /** Unknown error */ UNKNOWN = 'UNKNOWN', /** Configuration error */ CONFIGURATION = 'CONFIGURATION', /** Initialization error */ INITIALIZATION = 'INITIALIZATION', /** Feature not enabled */ FEATURE_NOT_ENABLED = 'FEATURE_NOT_ENABLED', /** Feature not found */ FEATURE_NOT_FOUND = 'FEATURE_NOT_FOUND', /** Invalid operation */ INVALID_OPERATION = 'INVALID_OPERATION', /** Platform error */ PLATFORM = 'PLATFORM', /** Binding error */ BINDING = 'BINDING', /** Network error */ NETWORK = 'NETWORK', /** Validation error */ VALIDATION = 'VALIDATION', } /** * OrbisError class * * Base error class for all Orbis1 SDK errors. * Provides structured error information with error codes and context. */ export class OrbisError extends Error { /** Error code */ public readonly code: OrbisErrorCode; /** Original error (if any) */ public readonly originalError?: Error; /** Additional context */ public readonly context?: Record; /** Timestamp when error occurred */ public readonly timestamp: number; /** * Create an OrbisError * * @param message - Error message * @param code - Error code * @param originalError - Original error (if any) * @param context - Additional context */ constructor( message: string, code: OrbisErrorCode = OrbisErrorCode.UNKNOWN, originalError?: Error, context?: Record ) { super(message); this.name = 'OrbisError'; this.code = code; this.originalError = originalError; this.context = context; this.timestamp = Date.now(); // Maintains proper stack trace for where our error was thrown (only available on V8) if (Error.captureStackTrace) { Error.captureStackTrace(this, OrbisError); } } /** * Convert error to JSON */ toJSON(): Record { return { name: this.name, message: this.message, code: this.code, timestamp: this.timestamp, context: this.context, stack: this.stack, originalError: this.originalError ? { name: this.originalError.name, message: this.originalError.message, stack: this.originalError.stack, } : undefined, }; } /** * Convert error to string */ toString(): string { let str = `${this.name} [${this.code}]: ${this.message}`; if (this.context && Object.keys(this.context).length > 0) { str += `\nContext: ${JSON.stringify(this.context)}`; } if (this.originalError) { str += `\nCaused by: ${this.originalError.message}`; } return str; } /** * Create an OrbisError from another error * * @param error - Original error * @param code - Error code * @param context - Additional context * @returns OrbisError instance */ static fromError( error: Error, code: OrbisErrorCode = OrbisErrorCode.UNKNOWN, context?: Record ): OrbisError { if (error instanceof OrbisError) { return error; } return new OrbisError(error.message, code, error, context); } /** * Check if an error is an OrbisError * * @param error - Error to check * @returns True if error is an OrbisError */ static isOrbisError(error: unknown): error is OrbisError { return error instanceof OrbisError; } /** * Check if an error has a specific code * * @param error - Error to check * @param code - Code to check for * @returns True if error has the specified code */ static hasCode(error: unknown, code: OrbisErrorCode): boolean { return OrbisError.isOrbisError(error) && error.code === code; } }