Skip to content

Commit

Permalink
added; path to CastErrors
Browse files Browse the repository at this point in the history
closes #1239
relates to #1232
  • Loading branch information
aheckmann committed Dec 10, 2012
1 parent 691ba87 commit 677476b
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 20 deletions.
5 changes: 3 additions & 2 deletions lib/errors/cast.js
Expand Up @@ -13,12 +13,13 @@ var MongooseError = require('../error');
* @api private
*/

function CastError (type, value) {
MongooseError.call(this, 'Cast to ' + type + ' failed for value "' + value + '"');
function CastError (type, value, path) {
MongooseError.call(this, 'Cast to ' + type + ' failed for value "' + value + '" at path "' + path + '"');
Error.captureStackTrace(this, arguments.callee);
this.name = 'CastError';
this.type = type;
this.value = value;
this.path = path;
};

/*!
Expand Down
10 changes: 7 additions & 3 deletions lib/schema/array.js
Expand Up @@ -46,6 +46,7 @@ function SchemaArray (key, cast, options) {
var caster = cast.name in Types ? Types[cast.name] : cast;
this.casterConstructor = caster;
this.caster = new caster(null, castOptions);
this.caster.path = key;
}

SchemaType.call(this, key, options);
Expand Down Expand Up @@ -121,7 +122,7 @@ SchemaArray.prototype.cast = function (value, doc, init) {
}
} catch (e) {
// rethrow
throw new CastError(e.type, value);
throw new CastError(e.type, value, this.path);
}
}

Expand Down Expand Up @@ -151,15 +152,18 @@ SchemaArray.prototype.castForQuery = function ($conditional, value) {
val = $conditional;
var proto = this.casterConstructor.prototype;
var method = proto.castForQuery || proto.cast;

var caster = this.caster;
if (Array.isArray(val)) {
val = val.map(function (v) {
if (method) v = method.call(proto, v);
if (method) v = method.call(caster, v);

return isMongooseObject(v)
? v.toObject()
: v;
});
} else if (method) {
val = method.call(proto, val);
val = method.call(caster, val);
}
}
return val && isMongooseObject(val)
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/buffer.js
Expand Up @@ -63,7 +63,7 @@ SchemaBuffer.prototype.cast = function (value, doc, init) {
return new MongooseBuffer(value, [this.path, doc]);
}

throw new CastError('buffer', value);
throw new CastError('buffer', value, this.path);
};

/*!
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/date.js
Expand Up @@ -63,7 +63,7 @@ SchemaDate.prototype.cast = function (value) {
if (date.toString() != 'Invalid Date')
return date;

throw new CastError('date', value);
throw new CastError('date', value, this.path);
};

/*!
Expand Down
1 change: 0 additions & 1 deletion lib/schema/documentarray.js
Expand Up @@ -7,7 +7,6 @@ var SchemaType = require('../schematype')
, ArrayType = require('./array')
, MongooseDocumentArray = require('../types/documentarray')
, Subdocument = require('../types/embedded')
, CastError = SchemaType.CastError
, Document = require('../document');

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/number.js
Expand Up @@ -124,7 +124,7 @@ SchemaNumber.prototype.cast = function (value, doc, init) {
}
}

throw new CastError('number', value);
throw new CastError('number', value, this.path);
};

/*!
Expand Down
11 changes: 8 additions & 3 deletions lib/schema/objectid.js
Expand Up @@ -61,10 +61,15 @@ ObjectId.prototype.cast = function (value, scope, init) {
if (value._id && value._id instanceof oid)
return value._id;

if (value.toString)
return oid.fromString(value.toString());
if (value.toString) {
try {
return oid.fromString(value.toString());
} catch (err) {
throw new CastError('ObjectId', value, this.path);
}
}

throw new CastError('object id', value);
throw new CastError('ObjectId', value, this.path);
};

/*!
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/string.js
Expand Up @@ -195,7 +195,7 @@ SchemaString.prototype.cast = function (value, scope, init) {
if (SchemaType._isRef(this, value, init)) return value;
if (value === null) return value;
if ('undefined' !== typeof value && value.toString) return value.toString();
throw new CastError('string', value);
throw new CastError('string', value, this.path);
};

/*!
Expand Down
3 changes: 1 addition & 2 deletions test/crash.test.js
Expand Up @@ -25,9 +25,8 @@ describe('crash: (gh-407)', function(){
}, function (err) {
db.close();

// should is acting strange
try {
assert.equal('Invalid ObjectId', err.message);
assert.equal('Cast to ObjectId failed for value "" at path "_id"', err.message);
} catch (er) {
console.error(err);
throw er;
Expand Down
6 changes: 3 additions & 3 deletions test/model.querying.test.js
Expand Up @@ -877,10 +877,10 @@ describe('model: querying:', function(){
assert.equal(1, nes1.length);

NE.find({ b: { $ne: [1] }}, function (err, nes2) {
assert.equal("Invalid ObjectId", err.message);
assert.equal("Cast to ObjectId failed for value \"1\" at path \"b\"", err.message);

NE.find({ b: { $ne: 4 }}, function (err, nes3) {
assert.equal("Invalid ObjectId", err.message);
assert.equal("Cast to ObjectId failed for value \"4\" at path \"b\"", err.message);

NE.find({ b: id3, ids: { $ne: id4 }}, function (err, nes4) {
db.close();
Expand Down Expand Up @@ -1689,7 +1689,7 @@ describe('buffers', function(){
assert.equal(rb.block.toString('utf8'),'buffer shtuffs are neat');

Test.findOne({ block: /buffer/i }, function (err, rb) {
assert.equal(err.message, 'Cast to buffer failed for value "/buffer/i"')
assert.equal(err.message, 'Cast to buffer failed for value "/buffer/i" at path "block"')
Test.findOne({ block: [195, 188, 98, 101, 114] }, function (err, rb) {
assert.ifError(err);
assert.equal(rb.block.toString('utf8'),'über');
Expand Down
2 changes: 1 addition & 1 deletion test/schema.test.js
Expand Up @@ -587,7 +587,7 @@ describe('schema', function(){
var M = db.model('castingStringArrayWithUndefined', schema);
M.find({ arr: { $in: [undefined] }}, function (err) {
db.close();
assert.equal(err && err.message, 'Cast to string failed for value "undefined"');
assert.equal(err && err.message, 'Cast to string failed for value "undefined" at path "arr"');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/types.buffer.test.js
Expand Up @@ -73,7 +73,7 @@ describe('types.buffer', function(){
assert.equal(err.name, 'CastError');
assert.equal(err.type, 'buffer');
assert.equal(err.value, 20);
assert.equal(err.message, 'Cast to buffer failed for value "20"');
assert.equal(err.message, 'Cast to buffer failed for value "20" at path "required"');
t.required = new Buffer("hello");

t.sub.push({ name: 'Friday Friday' });
Expand Down

0 comments on commit 677476b

Please sign in to comment.