diff --git a/package.json b/package.json index 42e1568..e62c53b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "monguito", - "version": "3.4.1", + "version": "4.0.0", "description": "MongoDB Abstract Repository implementation for Node.js", "author": { "name": "Josu Martinez", diff --git a/src/mongoose.repository.ts b/src/mongoose.repository.ts index ae84936..589b707 100644 --- a/src/mongoose.repository.ts +++ b/src/mongoose.repository.ts @@ -118,9 +118,12 @@ export abstract class MongooseRepository> `There is no document matching the given ID '${entity.id}'. New entities cannot not specify an ID`, ); } catch (error) { - if (error.message.includes('validation failed')) { + if ( + error.message.includes('validation failed') || + error.message.includes('duplicate key error') + ) { throw new ValidationException( - `Some fields of the given entity do not specify valid values`, + 'One or more fields of the given entity do not specify valid values', error, ); } @@ -174,18 +177,9 @@ export abstract class MongooseRepository> entity: S, userId?: string, ): Promise> { - try { - this.setDiscriminatorKeyOn(entity); - const document = this.createDocumentAndSetUserId(entity, userId); - return (await document.save()) as HydratedDocument; - } catch (error) { - if (error.message.includes('duplicate key error')) { - throw new ValidationException( - `The given entity includes a field which value is expected to be unique`, - ); - } - throw error; - } + this.setDiscriminatorKeyOn(entity); + const document = this.createDocumentAndSetUserId(entity, userId); + return (await document.save()) as HydratedDocument; } private setDiscriminatorKeyOn(