@aequum/typeorm
Version:
aequum TypeORM tools for repository, pagination, CRUD/CRUDL, configs and utils
24 lines (23 loc) • 937 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDuplicateError = isDuplicateError;
exports.duplicateEntryExceptionOrError = duplicateEntryExceptionOrError;
const typeorm_1 = require("typeorm");
const data_1 = require("@aequum/exceptions/data");
/**
* Check if the error is a TypeORMError and if it is a duplicate error
*/
function isDuplicateError(err) {
return (err instanceof typeorm_1.TypeORMError
&& 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.
*/
function duplicateEntryExceptionOrError(err, message, data, duplicatedProperties) {
if (isDuplicateError(err))
return new data_1.DuplicateEntryException(message, data, duplicatedProperties, err);
return err;
}