Cleanup
This commit is contained in:
40
node_modules/mongoose/lib/error/cast.js
generated
vendored
40
node_modules/mongoose/lib/error/cast.js
generated
vendored
@@ -22,8 +22,9 @@ class CastError extends MongooseError {
|
||||
// If no args, assume we'll `init()` later.
|
||||
if (arguments.length > 0) {
|
||||
const stringValue = getStringValue(value);
|
||||
const valueType = getValueType(value);
|
||||
const messageFormat = getMessageFormat(schemaType);
|
||||
const msg = formatMessage(null, type, stringValue, path, messageFormat);
|
||||
const msg = formatMessage(null, type, stringValue, path, messageFormat, valueType);
|
||||
super(msg);
|
||||
this.init(type, value, path, reason, schemaType);
|
||||
} else {
|
||||
@@ -31,6 +32,18 @@ class CastError extends MongooseError {
|
||||
}
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return {
|
||||
stringValue: this.stringValue,
|
||||
valueType: this.valueType,
|
||||
kind: this.kind,
|
||||
value: this.value,
|
||||
path: this.path,
|
||||
reason: this.reason,
|
||||
name: this.name,
|
||||
message: this.message
|
||||
};
|
||||
}
|
||||
/*!
|
||||
* ignore
|
||||
*/
|
||||
@@ -41,6 +54,7 @@ class CastError extends MongooseError {
|
||||
this.value = value;
|
||||
this.path = path;
|
||||
this.reason = reason;
|
||||
this.valueType = getValueType(value);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -55,6 +69,7 @@ class CastError extends MongooseError {
|
||||
this.path = other.path;
|
||||
this.reason = other.reason;
|
||||
this.message = other.message;
|
||||
this.valueType = other.valueType;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -63,7 +78,7 @@ class CastError extends MongooseError {
|
||||
setModel(model) {
|
||||
this.model = model;
|
||||
this.message = formatMessage(model, this.kind, this.stringValue, this.path,
|
||||
this.messageFormat);
|
||||
this.messageFormat, this.valueType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +95,21 @@ function getStringValue(value) {
|
||||
return stringValue;
|
||||
}
|
||||
|
||||
function getValueType(value) {
|
||||
if (value == null) {
|
||||
return '' + value;
|
||||
}
|
||||
|
||||
const t = typeof value;
|
||||
if (t !== 'object') {
|
||||
return t;
|
||||
}
|
||||
if (typeof value.constructor !== 'function') {
|
||||
return t;
|
||||
}
|
||||
return value.constructor.name;
|
||||
}
|
||||
|
||||
function getMessageFormat(schemaType) {
|
||||
const messageFormat = get(schemaType, 'options.cast', null);
|
||||
if (typeof messageFormat === 'string') {
|
||||
@@ -91,7 +121,7 @@ function getMessageFormat(schemaType) {
|
||||
* ignore
|
||||
*/
|
||||
|
||||
function formatMessage(model, kind, stringValue, path, messageFormat) {
|
||||
function formatMessage(model, kind, stringValue, path, messageFormat, valueType) {
|
||||
if (messageFormat != null) {
|
||||
let ret = messageFormat.
|
||||
replace('{KIND}', kind).
|
||||
@@ -103,12 +133,12 @@ function formatMessage(model, kind, stringValue, path, messageFormat) {
|
||||
|
||||
return ret;
|
||||
} else {
|
||||
const valueTypeMsg = valueType ? ' (type ' + valueType + ')' : '';
|
||||
let ret = 'Cast to ' + kind + ' failed for value ' +
|
||||
stringValue + ' at path "' + path + '"';
|
||||
stringValue + valueTypeMsg + ' at path "' + path + '"';
|
||||
if (model != null) {
|
||||
ret += ' for model "' + model.modelName + '"';
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user