docudb
Version:
Document-based NoSQL database for NodeJS
122 lines • 3.36 kB
JavaScript
/**
* Error handling module
* Provides standardized error codes and error classes
*/
// Error codes organized by module
const MCO_ERROR = {
DATABASE: {
INVALID_NAME: 'DB000',
INIT_ERROR: 'DB001',
COLLECTION_NOT_FOUND: 'DB002',
INVALID_COLLECTION_NAME: 'DB003',
COLLECTION_ALREADY_EXISTS: 'DB004',
DOCUMENT_NOT_FOUND: 'DB005',
INVALID_ID: 'DB006',
DUPLICATE_ID: 'DB007',
INVALID_QUERY: 'DB008',
INVALID_UPDATE: 'DB009',
TRANSACTION_ERROR: 'DB010',
NOT_INITIALIZED: 'DB011',
LOAD_ERROR: undefined,
COLLECTION_ERROR: undefined
},
SCHEMA: {
INVALID_DOCUMENT: 'SCH001',
REQUIRED_FIELD: 'SCH002',
INVALID_TYPE: 'SCH003',
VALIDATION_ERROR: 'SCH004',
INVALID_FIELD: 'SCH005',
CUSTOM_VALIDATION_ERROR: 'SCH006',
INVALID_ENUM: undefined,
INVALID_REGEX: undefined,
INVALID_LENGTH: undefined,
INVALID_VALUE: undefined
},
STORAGE: {
FILE_NOT_FOUND: 'STO001',
WRITE_ERROR: 'STO002',
READ_ERROR: 'STO003',
DELETE_ERROR: 'STO004',
COMPRESSION_ERROR: 'STO005',
SAVE_ERROR: undefined,
INIT_ERROR: undefined
},
INDEX: {
CREATE_ERROR: 'IDX001',
UPDATE_ERROR: 'IDX002',
DELETE_ERROR: 'IDX003',
UNIQUE_CONSTRAINT: 'IDX004',
LOAD_ERROR: undefined,
SAVE_ERROR: undefined,
UNIQUE_VIOLATION: undefined,
INIT_ERROR: undefined,
DROP_ERROR: undefined,
INVALID_FIELD_TYPE: 'IDX005'
},
DOCUMENT: {
NOT_FOUND: 'DOC001',
INSERT_ERROR: 'DOC002',
INVALID_TYPE: 'DOC003',
DELETE_ERROR: 'DOC004',
LOCK_ERROR: 'DOC005',
UPDATE_ERROR: 'DOC006',
QUERY_ERROR: 'DOC007',
INVALID_ID: 'DOC008',
INVALID_DOCUMENT: 'DOC009'
},
COLLECTION: {
METADATA_ERROR: 'COL001',
DROP_ERROR: 'COL002',
INVALID_NAME: 'COL003',
INSERT_ERROR: 'COL004'
},
COMPRESSION: {
INSERT_ERROR: 'COM001',
COMPRESS_ERROR: 'COM002'
},
QUERY: {
INSERT_ERROR: 'QUE001',
INVALID_OPERATOR: 'QUE002'
}
};
/**
* Class for handling DocuDB errors
* @extends Error
*/
class DocuDBError extends Error {
code;
details;
timestamp;
/**
* @param {string} message - Error message
* @param {string} code - Error code from MCO_ERROR
* @param {Object} details - Additional error details
*/
constructor(message, code, details = {}) {
super(message);
this.name = 'DocuDBError';
this.code = code;
this.details = details;
this.timestamp = new Date();
// Capture stack trace
if (Error.captureStackTrace !== undefined) {
Error.captureStackTrace(this, DocuDBError);
}
}
/**
* Converts the error to a JSON object
* @returns {Object} - JSON representation of the error
*/
toJSON() {
return {
name: this.name,
code: this.code,
message: this.message,
details: this.details,
timestamp: this.timestamp,
stack: this.stack
};
}
}
export { MCO_ERROR, DocuDBError };
//# sourceMappingURL=errors.js.map