Skip to content

Commit bba2ec2

Browse files
authored
Merge pull request #16096 from mahmoodhamdi/chore/issue-15995-standardize-errors-model
chore(model): use MongooseError instead of Error
2 parents 060af51 + c35cbf7 commit bba2ec2

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/model.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ Model.prototype.db;
190190

191191
Model.useConnection = function useConnection(connection) {
192192
if (!connection) {
193-
throw new Error('Please provide a connection.');
193+
throw new MongooseError('Please provide a connection.');
194194
}
195195
if (this.db) {
196196
delete this.db.models[this.modelName];
@@ -825,7 +825,7 @@ Model.prototype.deleteOne = function deleteOne(options) {
825825
// `self` is passed to pre hooks as argument for backwards compatibility, but that
826826
// isn't the actual arguments passed to the wrapped function.
827827
if (res[0] !== self || res[1] !== options) {
828-
throw new Error('Document deleteOne pre hooks cannot overwrite arguments');
828+
throw new MongooseError('Document deleteOne pre hooks cannot overwrite arguments');
829829
}
830830
query.deleteOne(where, options);
831831
// Apply custom where conditions _after_ document deleteOne middleware for
@@ -3610,7 +3610,7 @@ async function buildPreSavePromise(document, options) {
36103610
const preFilter = buildMiddlewareFilter(options, 'pre');
36113611
const [newOptions] = await document.schema.s.hooks.execPre('save', document, [options], { filter: preFilter });
36123612
if (newOptions !== options) {
3613-
throw new Error('Cannot overwrite options in pre("save") hook on bulkSave()');
3613+
throw new MongooseError('Cannot overwrite options in pre("save") hook on bulkSave()');
36143614
}
36153615
}
36163616

@@ -3850,15 +3850,15 @@ Model.castObject = function castObject(obj, options) {
38503850

38513851
Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, options) {
38523852
if (!Array.isArray(documents)) {
3853-
throw new Error(`bulkSave expects an array of documents to be passed, received \`${documents}\` instead`);
3853+
throw new MongooseError(`bulkSave expects an array of documents to be passed, received \`${documents}\` instead`);
38543854
}
38553855

38563856
setDefaultOptions();
38573857

38583858
const writeOperations = documents.map((document, i) => {
38593859
if (!options.skipValidation) {
38603860
if (!(document instanceof Document)) {
3861-
throw new Error(`documents.${i} was not a mongoose document, documents must be an array of mongoose documents (instanceof mongoose.Document).`);
3861+
throw new MongooseError(`documents.${i} was not a mongoose document, documents must be an array of mongoose documents (instanceof mongoose.Document).`);
38623862
}
38633863
if (options.validateBeforeSave == null || options.validateBeforeSave) {
38643864
const err = document.validateSync();
@@ -4949,7 +4949,7 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
49494949
Model.clientEncryption = function clientEncryption() {
49504950
const ClientEncryption = this.base.driver.get().ClientEncryption;
49514951
if (!ClientEncryption) {
4952-
throw new Error('The mongodb driver must be used to obtain a ClientEncryption object.');
4952+
throw new MongooseError('The mongodb driver must be used to obtain a ClientEncryption object.');
49534953
}
49544954

49554955
const client = this.collection?.conn?.client;

0 commit comments

Comments
 (0)