Skip to content

Commit

Permalink
consistent error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Nov 5, 2024
1 parent 15027c9 commit cd1d6da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
3 changes: 1 addition & 2 deletions lib/error/objectParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ const MongooseError = require('./mongooseError');
*/

class ObjectParameterError extends MongooseError {

constructor(value, paramName, fnName) {
super('Parameter "' + paramName + '" to ' + fnName +
'() must be an object, got "' + value.toString() + '" (type ' + typeof value + ')');
'() must be an object, got "' + (value == null ? value : value.toString()) + '" (type ' + typeof value + ')');
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,10 @@ Query.prototype._validateOp = function() {
if (this.op != null && !validOpsSet.has(this.op)) {
this.error(new Error('Query has invalid `op`: "' + this.op + '"'));
}

if (this.op !== 'estimatedDocumentCount' && this._conditions === null) {
throw new ObjectParameterError(this._conditions, 'filter', this.op);
}
};

/**
Expand Down
18 changes: 9 additions & 9 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4423,39 +4423,39 @@ describe('Query', function() {

await assert.rejects(
() => UserModel.find(null),
/MongoServerError: Expected field filterto be of type object/
/ObjectParameterError: Parameter "filter" to find\(\) must be an object, got "null"/
);
await assert.rejects(
() => UserModel.findOne(null),
/MongoServerError: Expected field filterto be of type object/
/ObjectParameterError: Parameter "filter" to findOne\(\) must be an object, got "null"/
);
await assert.rejects(
() => UserModel.findOneAndUpdate(null, { name: 'test2' }),
/MongoInvalidArgumentError: Argument "filter" must be an object/
/ObjectParameterError: Parameter "filter" to findOneAndUpdate\(\) must be an object, got "null"/
);
await assert.rejects(
() => UserModel.findOneAndReplace(null, { name: 'test2' }),
/MongoInvalidArgumentError: Argument "filter" must be an object/
/ObjectParameterError: Parameter "filter" to findOneAndReplace\(\) must be an object, got "null"/
);
await assert.rejects(
() => UserModel.findOneAndDelete(null),
/MongoInvalidArgumentError: Argument "filter" must be an object/
/ObjectParameterError: Parameter "filter" to findOneAndDelete\(\) must be an object, got "null"/
);
await assert.rejects(
() => UserModel.updateOne(null, { name: 'test2' }),
/MongoInvalidArgumentError: Selector must be a valid JavaScript object/
/ObjectParameterError: Parameter "filter" to updateOne\(\) must be an object, got "null"/
);
await assert.rejects(
() => UserModel.updateMany(null, { name: 'test2' }),
/MongoInvalidArgumentError: Selector must be a valid JavaScript object/
/ObjectParameterError: Parameter "filter" to updateMany\(\) must be an object, got "null"/
);
await assert.rejects(
() => UserModel.deleteOne(null),
/MongoServerError: BSON field 'delete.deletes.q' is missing but a required field/
/ObjectParameterError: Parameter "filter" to deleteOne\(\) must be an object, got "null"/
);
await assert.rejects(
() => UserModel.deleteMany(null),
/MongoServerError: BSON field 'delete.deletes.q' is missing but a required field/
/ObjectParameterError: Parameter "filter" to deleteMany\(\) must be an object, got "null"/
);
});
});

0 comments on commit cd1d6da

Please sign in to comment.