Commit
orFail()
Fix #7409
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,15 @@ const util = require('util'); | |
* @inherits MongooseError | ||
*/ | ||
|
||
function DocumentNotFoundError(query) { | ||
function DocumentNotFoundError(filter) { | ||
let msg; | ||
const messages = MongooseError.messages; | ||
if (messages.DocumentNotFoundError != null) { | ||
msg = typeof messages.DocumentNotFoundError === 'function' ? | ||
messages.DocumentNotFoundError(query) : | ||
messages.DocumentNotFoundError(filter) : | ||
messages.DocumentNotFoundError; | ||
} else { | ||
msg = 'No document found for query "' + util.inspect(query) + '"'; | ||
msg = 'No document found for query "' + util.inspect(filter) + '"'; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
vkarpov15
Author
Collaborator
|
||
} | ||
|
||
MongooseError.call(this, msg); | ||
|
@@ -33,7 +33,9 @@ function DocumentNotFoundError(query) { | |
this.stack = new Error().stack; | ||
} | ||
|
||
this.query = query; | ||
this.filter = filter; | ||
// Backwards compat | ||
this.query = filter; | ||
} | ||
|
||
/*! | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
*/ | ||
|
||
const CastError = require('./error/cast'); | ||
const DocumentNotFoundError = require('./error/notFound'); | ||
const Kareem = require('kareem'); | ||
const ObjectParameterError = require('./error/objectParameter'); | ||
const QueryCursor = require('./cursor/QueryCursor'); | ||
|
@@ -3903,7 +3904,7 @@ Query.prototype.map = function(fn) { | |
* @method orFail | ||
* @memberOf Query | ||
* @instance | ||
* @param {Function|Error} [err] optional error to throw if no docs match `filter` | ||
* @param {Function|Error} [err] optional error to throw if no docs match `filter`. If not specified, `orFail()` will throw a `DocumentNotFoundError` | ||
* @return {Query} this | ||
*/ | ||
|
||
|
@@ -3912,42 +3913,36 @@ Query.prototype.orFail = function(err) { | |
switch (this.op) { | ||
case 'find': | ||
if (res.length === 0) { | ||
err = typeof err === 'function' ? err.call(this) : err; | ||
throw err; | ||
throw _orFailError(err, this); | ||
} | ||
break; | ||
case 'findOne': | ||
if (res == null) { | ||
err = typeof err === 'function' ? err.call(this) : err; | ||
throw err; | ||
throw _orFailError(err, this); | ||
} | ||
break; | ||
case 'update': | ||
case 'updateMany': | ||
case 'updateOne': | ||
if (get(res, 'result.nModified') === 0) { | ||
err = typeof err === 'function' ? err.call(this) : err; | ||
throw err; | ||
throw _orFailError(err, this); | ||
} | ||
break; | ||
case 'findOneAndDelete': | ||
if (get(res, 'lastErrorObject.n') === 0) { | ||
err = typeof err === 'function' ? err.call(this) : err; | ||
throw err; | ||
throw _orFailError(err, this); | ||
} | ||
break; | ||
case 'findOneAndUpdate': | ||
if (get(res, 'lastErrorObject.updatedExisting') === false) { | ||
err = typeof err === 'function' ? err.call(this) : err; | ||
throw err; | ||
throw _orFailError(err, this); | ||
} | ||
break; | ||
case 'deleteMany': | ||
case 'deleteOne': | ||
case 'remove': | ||
if (res.n === 0) { | ||
err = typeof err === 'function' ? err.call(this) : err; | ||
throw err; | ||
throw _orFailError(err, this); | ||
} | ||
break; | ||
default: | ||
|
@@ -3959,6 +3954,22 @@ Query.prototype.orFail = function(err) { | |
return this; | ||
}; | ||
|
||
/*! | ||
* Get the error to throw for `orFail()` | ||
*/ | ||
|
||
function _orFailError(err, query) { | ||
if (typeof err === 'function') { | ||
err = err.call(query); | ||
} | ||
|
||
if (err == null) { | ||
This comment has been minimized.
Sorry, something went wrong.
piranna
|
||
err = new DocumentNotFoundError(query.getQuery()); | ||
} | ||
|
||
return err; | ||
} | ||
|
||
/** | ||
* Executes the query | ||
* | ||
|
Shouldn't the word
query
being changed tofilter
, too?