Skip to content

Commit

Permalink
Merge branch 'fix-8691'
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Mar 29, 2020
2 parents 088633f + ee135ef commit 0c09395
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
48 changes: 37 additions & 11 deletions lib/error/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@ const util = require('util');
*/

function CastError(type, value, path, reason, schemaType) {
// If no args, assume we'll `init()` later.
if (arguments.length > 0) {
this.init(type, value, path, reason, schemaType);
}

MongooseError.call(this, this.formatMessage());
this.name = 'CastError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
}

/*!
* Inherits from MongooseError.
*/

CastError.prototype = Object.create(MongooseError.prototype);
CastError.prototype.constructor = MongooseError;

/*!
* ignore
*/

CastError.prototype.init = function init(type, value, path, reason, schemaType) {
let stringValue = util.inspect(value);
stringValue = stringValue.replace(/^'/, '"').replace(/'$/, '"');
if (!stringValue.startsWith('"')) {
Expand All @@ -33,21 +59,21 @@ function CastError(type, value, path, reason, schemaType) {
this.value = value;
this.path = path;
this.reason = reason;
MongooseError.call(this, this.formatMessage());
this.name = 'CastError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
}
};

/*!
* Inherits from MongooseError.
* ignore
*/

CastError.prototype = Object.create(MongooseError.prototype);
CastError.prototype.constructor = MongooseError;
CastError.prototype.copy = function copy(other) {
this.messageFormat = other.messageFormat;
this.stringValue = other.stringValue;
this.kind = other.type;
this.value = other.value;
this.path = other.path;
this.reason = other.reason;
this.message = other.message;
};

/*!
* ignore
Expand Down
22 changes: 19 additions & 3 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4326,6 +4326,9 @@ function _orFailError(err, query) {

Query.prototype.exec = function exec(op, callback) {
const _this = this;
// Ensure that `exec()` is the first thing that shows up in
// the stack when cast errors happen.
const castError = new CastError();

if (typeof op === 'function') {
callback = op;
Expand All @@ -4346,7 +4349,7 @@ Query.prototype.exec = function exec(op, callback) {

this._hooks.execPre('exec', this, [], (error) => {
if (error != null) {
return cb(error);
return cb(_cleanCastErrorStack(castError, error));
}
let thunk = '_' + this.op;
if (this.op === 'update') {
Expand All @@ -4356,12 +4359,12 @@ Query.prototype.exec = function exec(op, callback) {
}
this[thunk].call(this, (error, res) => {
if (error) {
return cb(error);
return cb(_cleanCastErrorStack(castError, error));
}

this._hooks.execPost('exec', this, [], {}, (error) => {
if (error) {
return cb(error);
return cb(_cleanCastErrorStack(castError, error));
}

cb(null, res);
Expand All @@ -4371,6 +4374,19 @@ Query.prototype.exec = function exec(op, callback) {
}, this.model.events);
};

/*!
* ignore
*/

function _cleanCastErrorStack(castError, error) {
if (error instanceof CastError) {
castError.copy(error);
return castError;
}

return error;
}

/*!
* ignore
*/
Expand Down

0 comments on commit 0c09395

Please sign in to comment.