-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(model): add throwOnValidationError option for opting into getting MongooseBulkWriteError if all valid operations succeed in bulkWrite()
and insertMany()
#13410
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd argue that this should default to
true
, since this is the intuitive behavior anyway, we can consider this as a bug rather than a missing feature. People should opt-out if they want backward-compatible behavior for some reason, then on v8.0 we can remove the option altogether.What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that we have tests that assert on the counterintuitive behavior, and for stability's sake I like to avoid changing existing tests outside of major releases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case I think it's good to merge as is, and remove the option in v8.0.
What do you think? Or would you rather to keep it as an option in v8, and maybe just change the default?