/** * Base error class for all CongoDB errors * Mirrors MongoDB driver error hierarchy */ export class CongoError extends Error { public code?: number; public codeName?: string; constructor(message: string, code?: number, codeName?: string) { super(message); this.name = 'CongoError'; this.code = code; this.codeName = codeName; Object.setPrototypeOf(this, new.target.prototype); } } /** * Error thrown during connection issues */ export class CongoConnectionError extends CongoError { constructor(message: string) { super(message); this.name = 'CongoConnectionError'; } } /** * Error thrown when an operation times out */ export class CongoTimeoutError extends CongoError { constructor(message: string) { super(message, 50, 'MaxTimeMSExpired'); this.name = 'CongoTimeoutError'; } } /** * Error thrown during write operations */ export class CongoWriteError extends CongoError { public writeErrors?: IWriteError[]; public result?: any; constructor(message: string, code?: number, writeErrors?: IWriteError[]) { super(message, code); this.name = 'CongoWriteError'; this.writeErrors = writeErrors; } } /** * Error thrown for duplicate key violations */ export class CongoDuplicateKeyError extends CongoWriteError { public keyPattern?: Record; public keyValue?: Record; constructor(message: string, keyPattern?: Record, keyValue?: Record) { super(message, 11000); this.name = 'CongoDuplicateKeyError'; this.codeName = 'DuplicateKey'; this.keyPattern = keyPattern; this.keyValue = keyValue; } } /** * Error thrown for bulk write failures */ export class CongoBulkWriteError extends CongoError { public writeErrors: IWriteError[]; public result: any; constructor(message: string, writeErrors: IWriteError[], result: any) { super(message, 65); this.name = 'CongoBulkWriteError'; this.writeErrors = writeErrors; this.result = result; } } /** * Error thrown during transaction operations */ export class CongoTransactionError extends CongoError { constructor(message: string, code?: number) { super(message, code); this.name = 'CongoTransactionError'; } } /** * Error thrown when a transaction is aborted due to conflict */ export class CongoWriteConflictError extends CongoTransactionError { constructor(message: string = 'Write conflict during transaction') { super(message, 112); this.name = 'CongoWriteConflictError'; this.codeName = 'WriteConflict'; } } /** * Error thrown for invalid arguments */ export class CongoArgumentError extends CongoError { constructor(message: string) { super(message); this.name = 'CongoArgumentError'; } } /** * Error thrown when an operation is not supported */ export class CongoNotSupportedError extends CongoError { constructor(message: string) { super(message, 115); this.name = 'CongoNotSupportedError'; this.codeName = 'CommandNotSupported'; } } /** * Error thrown when cursor is exhausted or closed */ export class CongoCursorError extends CongoError { constructor(message: string) { super(message); this.name = 'CongoCursorError'; } } /** * Error thrown when a namespace (database.collection) is invalid */ export class CongoNamespaceError extends CongoError { constructor(message: string) { super(message, 73); this.name = 'CongoNamespaceError'; this.codeName = 'InvalidNamespace'; } } /** * Error thrown when an index operation fails */ export class CongoIndexError extends CongoError { constructor(message: string, code?: number) { super(message, code || 86); this.name = 'CongoIndexError'; } } /** * Write error detail for bulk operations */ export interface IWriteError { index: number; code: number; errmsg: string; op: any; } /** * Convert any error to a CongoError */ export function toCongoError(error: any): CongoError { if (error instanceof CongoError) { return error; } const congoError = new CongoError(error.message || String(error)); if (error.code) { congoError.code = error.code; } if (error.codeName) { congoError.codeName = error.codeName; } return congoError; }