This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -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;
}
}

View File

@@ -3,7 +3,7 @@
* The default built-in validator error messages. These may be customized.
*
* // customize within each schema or globally like so
* var mongoose = require('mongoose');
* const mongoose = require('mongoose');
* mongoose.Error.messages.String.enum = "Your custom message for {PATH}.";
*
* As you might have noticed, error messages support basic templating

View File

@@ -7,6 +7,7 @@
const MongooseError = require('./mongooseError');
const allServersUnknown = require('../helpers/topology/allServersUnknown');
const isAtlas = require('../helpers/topology/isAtlas');
const isSSLError = require('../helpers/topology/isSSLError');
/*!
* ignore
@@ -17,6 +18,11 @@ const atlasMessage = 'Could not connect to any servers in your MongoDB Atlas clu
'an IP that isn\'t whitelisted. Make sure your current IP address is on your Atlas ' +
'cluster\'s IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/';
const sslMessage = 'Mongoose is connecting with SSL enabled, but the server is ' +
'not accepting SSL connections. Please ensure that the MongoDB server you are ' +
'connecting to is configured to accept SSL connections. Learn more: ' +
'https://mongoosejs.com/docs/tutorials/ssl.html';
class MongooseServerSelectionError extends MongooseError {
/**
* MongooseServerSelectionError constructor
@@ -28,10 +34,16 @@ class MongooseServerSelectionError extends MongooseError {
// Special message for a case that is likely due to IP whitelisting issues.
const isAtlasWhitelistError = isAtlas(reason) &&
allServersUnknown(reason) &&
err.message.indexOf('bad auth') === -1;
this.message = isAtlasWhitelistError ?
atlasMessage :
err.message;
err.message.indexOf('bad auth') === -1 &&
err.message.indexOf('Authentication failed') === -1;
if (isAtlasWhitelistError) {
this.message = atlasMessage;
} else if (isSSLError(reason)) {
this.message = sslMessage;
} else {
this.message = err.message;
}
for (const key in err) {
if (key !== 'name') {
this[key] = err[key];

View File

@@ -4,10 +4,10 @@
'use strict';
const MongooseError = require('./');
const MongooseError = require('./mongooseError');
const getConstructorName = require('../helpers/getConstructorName');
const util = require('util');
class ValidationError extends MongooseError {
/**
* Document Validation Error
@@ -18,7 +18,7 @@ class ValidationError extends MongooseError {
*/
constructor(instance) {
let _message;
if (instance && instance.constructor.name === 'model') {
if (getConstructorName(instance) === 'model') {
_message = instance.constructor.modelName + ' validation failed';
} else {
_message = 'Validation failed';
@@ -68,13 +68,14 @@ if (util.inspect.custom) {
/*!
* Helper for JSON.stringify
* Ensure `name` and `message` show up in toJSON output re: gh-9847
*/
Object.defineProperty(ValidationError.prototype, 'toJSON', {
enumerable: false,
writable: false,
configurable: true,
value: function() {
return Object.assign({}, this, { message: this.message });
return Object.assign({}, this, { name: this.name, message: this.message });
}
});
@@ -108,4 +109,4 @@ function _generateMessage(err) {
* Module exports
*/
module.exports = exports = ValidationError;
module.exports = ValidationError;

View File

@@ -38,6 +38,14 @@ class ValidatorError extends MongooseError {
toString() {
return this.message;
}
/*!
* Ensure `name` and `message` show up in toJSON output re: gh-9296
*/
toJSON() {
return Object.assign({ name: this.name, message: this.message }, this);
}
}