@aequum/mongoose
Version:
aequum mongoose tools for repository, pagination, CRUD/CRUDL, configs and utils
35 lines (34 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDuplicateError = isDuplicateError;
exports.duplicateEntryExceptionOrError = duplicateEntryExceptionOrError;
const data_1 = require("@aequum/exceptions/data");
/**
* Check whether if the error is a duplicate entry error from
* MongoDB.
*
* @param err - Error to check
* @returns
*/
function isDuplicateError(err) {
return (err.name === 'MongoServerError' && err.code === 11000
&& err.message.match(/(duplicate|unique (key|constraint)|primary key|E11000)/i));
}
/**
* Check if the error is a duplicate entry error, if it
* is, returns a `new DuplicateEntryException` with
* passed arguments, otherwise returns the orignal error.
*
* @param err Error to check
* @param message Message to be used in the exception
* @param data Data to be shown in the exception
* @param duplicatedProperties Duplicated properties
* to be shown in the exeption
*
* @returns DuplicateEntryException|Error
*/
function duplicateEntryExceptionOrError(err, message, data, duplicatedProperties) {
if (isDuplicateError(err))
return new data_1.DuplicateEntryException(message, data, duplicatedProperties, err);
return err;
}