182 lines
4.3 KiB
TypeScript
182 lines
4.3 KiB
TypeScript
|
|
/**
|
||
|
|
* Base error class for all SmartDB errors
|
||
|
|
* Mirrors MongoDB driver error hierarchy
|
||
|
|
*/
|
||
|
|
export class SmartdbError extends Error {
|
||
|
|
public code?: number;
|
||
|
|
public codeName?: string;
|
||
|
|
|
||
|
|
constructor(message: string, code?: number, codeName?: string) {
|
||
|
|
super(message);
|
||
|
|
this.name = 'SmartdbError';
|
||
|
|
this.code = code;
|
||
|
|
this.codeName = codeName;
|
||
|
|
Object.setPrototypeOf(this, new.target.prototype);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown during connection issues
|
||
|
|
*/
|
||
|
|
export class SmartdbConnectionError extends SmartdbError {
|
||
|
|
constructor(message: string) {
|
||
|
|
super(message);
|
||
|
|
this.name = 'SmartdbConnectionError';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown when an operation times out
|
||
|
|
*/
|
||
|
|
export class SmartdbTimeoutError extends SmartdbError {
|
||
|
|
constructor(message: string) {
|
||
|
|
super(message, 50, 'MaxTimeMSExpired');
|
||
|
|
this.name = 'SmartdbTimeoutError';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown during write operations
|
||
|
|
*/
|
||
|
|
export class SmartdbWriteError extends SmartdbError {
|
||
|
|
public writeErrors?: IWriteError[];
|
||
|
|
public result?: any;
|
||
|
|
|
||
|
|
constructor(message: string, code?: number, writeErrors?: IWriteError[]) {
|
||
|
|
super(message, code);
|
||
|
|
this.name = 'SmartdbWriteError';
|
||
|
|
this.writeErrors = writeErrors;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown for duplicate key violations
|
||
|
|
*/
|
||
|
|
export class SmartdbDuplicateKeyError extends SmartdbWriteError {
|
||
|
|
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 = 'SmartdbDuplicateKeyError';
|
||
|
|
this.codeName = 'DuplicateKey';
|
||
|
|
this.keyPattern = keyPattern;
|
||
|
|
this.keyValue = keyValue;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown for bulk write failures
|
||
|
|
*/
|
||
|
|
export class SmartdbBulkWriteError extends SmartdbError {
|
||
|
|
public writeErrors: IWriteError[];
|
||
|
|
public result: any;
|
||
|
|
|
||
|
|
constructor(message: string, writeErrors: IWriteError[], result: any) {
|
||
|
|
super(message, 65);
|
||
|
|
this.name = 'SmartdbBulkWriteError';
|
||
|
|
this.writeErrors = writeErrors;
|
||
|
|
this.result = result;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown during transaction operations
|
||
|
|
*/
|
||
|
|
export class SmartdbTransactionError extends SmartdbError {
|
||
|
|
constructor(message: string, code?: number) {
|
||
|
|
super(message, code);
|
||
|
|
this.name = 'SmartdbTransactionError';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown when a transaction is aborted due to conflict
|
||
|
|
*/
|
||
|
|
export class SmartdbWriteConflictError extends SmartdbTransactionError {
|
||
|
|
constructor(message: string = 'Write conflict during transaction') {
|
||
|
|
super(message, 112);
|
||
|
|
this.name = 'SmartdbWriteConflictError';
|
||
|
|
this.codeName = 'WriteConflict';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown for invalid arguments
|
||
|
|
*/
|
||
|
|
export class SmartdbArgumentError extends SmartdbError {
|
||
|
|
constructor(message: string) {
|
||
|
|
super(message);
|
||
|
|
this.name = 'SmartdbArgumentError';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown when an operation is not supported
|
||
|
|
*/
|
||
|
|
export class SmartdbNotSupportedError extends SmartdbError {
|
||
|
|
constructor(message: string) {
|
||
|
|
super(message, 115);
|
||
|
|
this.name = 'SmartdbNotSupportedError';
|
||
|
|
this.codeName = 'CommandNotSupported';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown when cursor is exhausted or closed
|
||
|
|
*/
|
||
|
|
export class SmartdbCursorError extends SmartdbError {
|
||
|
|
constructor(message: string) {
|
||
|
|
super(message);
|
||
|
|
this.name = 'SmartdbCursorError';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown when a namespace (database.collection) is invalid
|
||
|
|
*/
|
||
|
|
export class SmartdbNamespaceError extends SmartdbError {
|
||
|
|
constructor(message: string) {
|
||
|
|
super(message, 73);
|
||
|
|
this.name = 'SmartdbNamespaceError';
|
||
|
|
this.codeName = 'InvalidNamespace';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Error thrown when an index operation fails
|
||
|
|
*/
|
||
|
|
export class SmartdbIndexError extends SmartdbError {
|
||
|
|
constructor(message: string, code?: number) {
|
||
|
|
super(message, code || 86);
|
||
|
|
this.name = 'SmartdbIndexError';
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Write error detail for bulk operations
|
||
|
|
*/
|
||
|
|
export interface IWriteError {
|
||
|
|
index: number;
|
||
|
|
code: number;
|
||
|
|
errmsg: string;
|
||
|
|
op: any;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Convert any error to a SmartdbError
|
||
|
|
*/
|
||
|
|
export function toSmartdbError(error: any): SmartdbError {
|
||
|
|
if (error instanceof SmartdbError) {
|
||
|
|
return error;
|
||
|
|
}
|
||
|
|
const smartdbError = new SmartdbError(error.message || String(error));
|
||
|
|
if (error.code) {
|
||
|
|
smartdbError.code = error.code;
|
||
|
|
}
|
||
|
|
if (error.codeName) {
|
||
|
|
smartdbError.codeName = error.codeName;
|
||
|
|
}
|
||
|
|
return smartdbError;
|
||
|
|
}
|