Files

182 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

/**
* Base error class for all TsmDB errors
* Mirrors MongoDB driver error hierarchy
*/
export class TsmdbError extends Error {
public code?: number;
public codeName?: string;
constructor(message: string, code?: number, codeName?: string) {
super(message);
this.name = 'TsmdbError';
this.code = code;
this.codeName = codeName;
Object.setPrototypeOf(this, new.target.prototype);
}
}
/**
* Error thrown during connection issues
*/
export class TsmdbConnectionError extends TsmdbError {
constructor(message: string) {
super(message);
this.name = 'TsmdbConnectionError';
}
}
/**
* Error thrown when an operation times out
*/
export class TsmdbTimeoutError extends TsmdbError {
constructor(message: string) {
super(message, 50, 'MaxTimeMSExpired');
this.name = 'TsmdbTimeoutError';
}
}
/**
* Error thrown during write operations
*/
export class TsmdbWriteError extends TsmdbError {
public writeErrors?: IWriteError[];
public result?: any;
constructor(message: string, code?: number, writeErrors?: IWriteError[]) {
super(message, code);
this.name = 'TsmdbWriteError';
this.writeErrors = writeErrors;
}
}
/**
* Error thrown for duplicate key violations
*/
export class TsmdbDuplicateKeyError extends TsmdbWriteError {
public keyPattern?: Record<string, 1>;
public keyValue?: Record<string, any>;
constructor(message: string, keyPattern?: Record<string, 1>, keyValue?: Record<string, any>) {
super(message, 11000);
this.name = 'TsmdbDuplicateKeyError';
this.codeName = 'DuplicateKey';
this.keyPattern = keyPattern;
this.keyValue = keyValue;
}
}
/**
* Error thrown for bulk write failures
*/
export class TsmdbBulkWriteError extends TsmdbError {
public writeErrors: IWriteError[];
public result: any;
constructor(message: string, writeErrors: IWriteError[], result: any) {
super(message, 65);
this.name = 'TsmdbBulkWriteError';
this.writeErrors = writeErrors;
this.result = result;
}
}
/**
* Error thrown during transaction operations
*/
export class TsmdbTransactionError extends TsmdbError {
constructor(message: string, code?: number) {
super(message, code);
this.name = 'TsmdbTransactionError';
}
}
/**
* Error thrown when a transaction is aborted due to conflict
*/
export class TsmdbWriteConflictError extends TsmdbTransactionError {
constructor(message: string = 'Write conflict during transaction') {
super(message, 112);
this.name = 'TsmdbWriteConflictError';
this.codeName = 'WriteConflict';
}
}
/**
* Error thrown for invalid arguments
*/
export class TsmdbArgumentError extends TsmdbError {
constructor(message: string) {
super(message);
this.name = 'TsmdbArgumentError';
}
}
/**
* Error thrown when an operation is not supported
*/
export class TsmdbNotSupportedError extends TsmdbError {
constructor(message: string) {
super(message, 115);
this.name = 'TsmdbNotSupportedError';
this.codeName = 'CommandNotSupported';
}
}
/**
* Error thrown when cursor is exhausted or closed
*/
export class TsmdbCursorError extends TsmdbError {
constructor(message: string) {
super(message);
this.name = 'TsmdbCursorError';
}
}
/**
* Error thrown when a namespace (database.collection) is invalid
*/
export class TsmdbNamespaceError extends TsmdbError {
constructor(message: string) {
super(message, 73);
this.name = 'TsmdbNamespaceError';
this.codeName = 'InvalidNamespace';
}
}
/**
* Error thrown when an index operation fails
*/
export class TsmdbIndexError extends TsmdbError {
constructor(message: string, code?: number) {
super(message, code || 86);
this.name = 'TsmdbIndexError';
}
}
/**
* Write error detail for bulk operations
*/
export interface IWriteError {
index: number;
code: number;
errmsg: string;
op: any;
}
/**
* Convert any error to a TsmdbError
*/
export function toTsmdbError(error: any): TsmdbError {
if (error instanceof TsmdbError) {
return error;
}
const tsmdbError = new TsmdbError(error.message || String(error));
if (error.code) {
tsmdbError.code = error.code;
}
if (error.codeName) {
tsmdbError.codeName = error.codeName;
}
return tsmdbError;
}