-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(model): add throwOnValidationError option for opting into getting MongooseBulkWriteError if all valid operations succeed in bulkWrite() and insertMany()
- Loading branch information
Showing
4 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*! | ||
* Module dependencies. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const MongooseError = require('./'); | ||
|
||
|
||
/** | ||
* If `bulkWrite()` or `insertMany()` has validation errors, but | ||
* all valid operations succeed, and 'throwOnValidationError' is true, | ||
* Mongoose will throw this error. | ||
* | ||
* @api private | ||
*/ | ||
|
||
class MongooseBulkWriteError extends MongooseError { | ||
constructor(validationErrors, results, rawResult, operation) { | ||
let preview = validationErrors.map(e => e.message).join(', '); | ||
if (preview.length > 200) { | ||
preview = preview.slice(0, 200) + '...'; | ||
} | ||
super(`${operation} failed with ${validationErrors.length} Mongoose validation errors: ${preview}`); | ||
|
||
this.validationErrors = validationErrors; | ||
this.results = results; | ||
this.rawResult = rawResult; | ||
this.operation = operation; | ||
} | ||
} | ||
|
||
Object.defineProperty(MongooseBulkWriteError.prototype, 'name', { | ||
value: 'MongooseBulkWriteError' | ||
}); | ||
|
||
/*! | ||
* exports | ||
*/ | ||
|
||
module.exports = MongooseBulkWriteError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters