This commit is contained in:
root
2019-11-28 20:40:02 +00:00
commit 1c78566f5b
2275 changed files with 272351 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/*!
* MissingSchema Error constructor.
*
* @inherits MongooseError
*/
function MissingSchemaError() {
var msg = 'Schema hasn\'t been registered for document.\n'
+ 'Use mongoose.Document(name, schema)';
MongooseError.call(this, msg);
this.name = 'MissingSchemaError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
}
/*!
* Inherits from MongooseError.
*/
MissingSchemaError.prototype = Object.create(MongooseError.prototype);
MissingSchemaError.prototype.constructor = MongooseError;
/*!
* exports
*/
module.exports = MissingSchemaError;
+60
View File
@@ -0,0 +1,60 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
var util = require('util');
/**
* Casting Error constructor.
*
* @param {String} type
* @param {String} value
* @inherits MongooseError
* @api private
*/
function CastError(type, value, path, reason) {
var stringValue = util.inspect(value);
stringValue = stringValue.replace(/^'/, '"').replace(/'$/, '"');
if (stringValue.charAt(0) !== '"') {
stringValue = '"' + stringValue + '"';
}
MongooseError.call(this, 'Cast to ' + type + ' failed for value ' +
stringValue + ' at path "' + path + '"');
this.name = 'CastError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
this.stringValue = stringValue;
this.kind = type;
this.value = value;
this.path = path;
this.reason = reason;
}
/*!
* Inherits from MongooseError.
*/
CastError.prototype = Object.create(MongooseError.prototype);
CastError.prototype.constructor = MongooseError;
/*!
* ignore
*/
CastError.prototype.setModel = function(model) {
this.model = model;
this.message = 'Cast to ' + this.kind + ' failed for value ' +
this.stringValue + ' at path "' + this.path + '"' + ' for model "' +
model.modelName + '"';
};
/*!
* exports
*/
module.exports = CastError;
+40
View File
@@ -0,0 +1,40 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/**
* Casting Error constructor.
*
* @param {String} type
* @param {String} value
* @inherits MongooseError
* @api private
*/
function DisconnectedError(connectionString) {
MongooseError.call(this, 'Ran out of retries trying to reconnect to "' +
connectionString + '". Try setting `server.reconnectTries` and ' +
'`server.reconnectInterval` to something higher.');
this.name = 'DisconnectedError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
}
/*!
* Inherits from MongooseError.
*/
DisconnectedError.prototype = Object.create(MongooseError.prototype);
DisconnectedError.prototype.constructor = MongooseError;
/*!
* exports
*/
module.exports = DisconnectedError;
+46
View File
@@ -0,0 +1,46 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/*!
* DivergentArrayError constructor.
*
* @inherits MongooseError
*/
function DivergentArrayError(paths) {
var msg = 'For your own good, using `document.save()` to update an array '
+ 'which was selected using an $elemMatch projection OR '
+ 'populated using skip, limit, query conditions, or exclusion of '
+ 'the _id field when the operation results in a $pop or $set of '
+ 'the entire array is not supported. The following '
+ 'path(s) would have been modified unsafely:\n'
+ ' ' + paths.join('\n ') + '\n'
+ 'Use Model.update() to update these arrays instead.';
// TODO write up a docs page (FAQ) and link to it
MongooseError.call(this, msg);
this.name = 'DivergentArrayError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
}
/*!
* Inherits from MongooseError.
*/
DivergentArrayError.prototype = Object.create(MongooseError.prototype);
DivergentArrayError.prototype.constructor = MongooseError;
/*!
* exports
*/
module.exports = DivergentArrayError;
+66
View File
@@ -0,0 +1,66 @@
/**
* MongooseError constructor
*
* @param {String} msg Error message
* @inherits Error https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error
*/
function MongooseError(msg) {
Error.call(this);
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
this.message = msg;
this.name = 'MongooseError';
}
/*!
* Inherits from Error.
*/
MongooseError.prototype = Object.create(Error.prototype);
MongooseError.prototype.constructor = Error;
/*!
* Module exports.
*/
module.exports = exports = MongooseError;
/**
* The default built-in validator error messages.
*
* @see Error.messages #error_messages_MongooseError-messages
* @api public
*/
MongooseError.messages = require('./messages');
// backward compat
MongooseError.Messages = MongooseError.messages;
/**
* This error will be called when `save()` fails because the underlying
* document was not found. The constructor takes one parameter, the
* conditions that mongoose passed to `update()` when trying to update
* the document.
*
* @api public
*/
MongooseError.DocumentNotFoundError = require('./notFound');
/*!
* Expose subclasses
*/
MongooseError.CastError = require('./cast');
MongooseError.ValidationError = require('./validation');
MongooseError.ValidatorError = require('./validator');
MongooseError.VersionError = require('./version');
MongooseError.OverwriteModelError = require('./overwriteModel');
MongooseError.MissingSchemaError = require('./missingSchema');
MongooseError.DivergentArrayError = require('./divergentArray');
+44
View File
@@ -0,0 +1,44 @@
/**
* The default built-in validator error messages. These may be customized.
*
* // customize within each schema or globally like so
* var mongoose = require('mongoose');
* mongoose.Error.messages.String.enum = "Your custom message for {PATH}.";
*
* As you might have noticed, error messages support basic templating
*
* - `{PATH}` is replaced with the invalid document path
* - `{VALUE}` is replaced with the invalid value
* - `{TYPE}` is replaced with the validator type such as "regexp", "min", or "user defined"
* - `{MIN}` is replaced with the declared min value for the Number.min validator
* - `{MAX}` is replaced with the declared max value for the Number.max validator
*
* Click the "show code" link below to see all defaults.
*
* @static messages
* @receiver MongooseError
* @api public
*/
var msg = module.exports = exports = {};
msg.DocumentNotFoundError = null;
msg.general = {};
msg.general.default = 'Validator failed for path `{PATH}` with value `{VALUE}`';
msg.general.required = 'Path `{PATH}` is required.';
msg.Number = {};
msg.Number.min = 'Path `{PATH}` ({VALUE}) is less than minimum allowed value ({MIN}).';
msg.Number.max = 'Path `{PATH}` ({VALUE}) is more than maximum allowed value ({MAX}).';
msg.Date = {};
msg.Date.min = 'Path `{PATH}` ({VALUE}) is before minimum allowed value ({MIN}).';
msg.Date.max = 'Path `{PATH}` ({VALUE}) is after maximum allowed value ({MAX}).';
msg.String = {};
msg.String.enum = '`{VALUE}` is not a valid enum value for path `{PATH}`.';
msg.String.match = 'Path `{PATH}` is invalid ({VALUE}).';
msg.String.minlength = 'Path `{PATH}` (`{VALUE}`) is shorter than the minimum allowed length ({MINLENGTH}).';
msg.String.maxlength = 'Path `{PATH}` (`{VALUE}`) is longer than the maximum allowed length ({MAXLENGTH}).';
+37
View File
@@ -0,0 +1,37 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/*!
* MissingSchema Error constructor.
*
* @inherits MongooseError
*/
function MissingSchemaError(name) {
var msg = 'Schema hasn\'t been registered for model "' + name + '".\n'
+ 'Use mongoose.model(name, schema)';
MongooseError.call(this, msg);
this.name = 'MissingSchemaError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
}
/*!
* Inherits from MongooseError.
*/
MissingSchemaError.prototype = Object.create(MongooseError.prototype);
MissingSchemaError.prototype.constructor = MongooseError;
/*!
* exports
*/
module.exports = MissingSchemaError;
+50
View File
@@ -0,0 +1,50 @@
'use strict';
/*!
* Module dependencies.
*/
var MongooseError = require('./');
var util = require('util');
/*!
* OverwriteModel Error constructor.
*
* @inherits MongooseError
*/
function DocumentNotFoundError(query) {
var msg;
var messages = MongooseError.messages;
if (messages.DocumentNotFoundError != null) {
msg = typeof messages.DocumentNotFoundError === 'function' ?
messages.DocumentNotFoundError(query) :
messages.DocumentNotFoundError;
} else {
msg = 'No document found for query "' + util.inspect(query) + '"';
}
MongooseError.call(this, msg);
this.name = 'DocumentNotFoundError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
this.query = query;
}
/*!
* Inherits from MongooseError.
*/
DocumentNotFoundError.prototype = Object.create(MongooseError.prototype);
DocumentNotFoundError.prototype.constructor = MongooseError;
/*!
* exports
*/
module.exports = DocumentNotFoundError;
+35
View File
@@ -0,0 +1,35 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/**
* Strict mode error constructor
*
* @param {String} type
* @param {String} value
* @inherits MongooseError
* @api private
*/
function ObjectExpectedError(path, val) {
MongooseError.call(this, 'Tried to set nested object field `' + path +
'` to primitive value `' + val + '` and strict mode is set to throw.');
this.name = 'ObjectExpectedError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
this.path = path;
}
/*!
* Inherits from MongooseError.
*/
ObjectExpectedError.prototype = Object.create(MongooseError.prototype);
ObjectExpectedError.prototype.constructor = MongooseError;
module.exports = ObjectExpectedError;
+36
View File
@@ -0,0 +1,36 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/**
* Constructor for errors that happen when a parameter that's expected to be
* an object isn't an object
*
* @param {Any} value
* @param {String} paramName
* @param {String} fnName
* @inherits MongooseError
* @api private
*/
function ObjectParameterError(value, paramName, fnName) {
MongooseError.call(this, 'Parameter "' + paramName + '" to ' + fnName +
'() must be an object, got ' + value.toString());
this.name = 'ObjectParameterError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
}
/*!
* Inherits from MongooseError.
*/
ObjectParameterError.prototype = Object.create(MongooseError.prototype);
ObjectParameterError.prototype.constructor = MongooseError;
module.exports = ObjectParameterError;
+35
View File
@@ -0,0 +1,35 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/*!
* OverwriteModel Error constructor.
*
* @inherits MongooseError
*/
function OverwriteModelError(name) {
MongooseError.call(this, 'Cannot overwrite `' + name + '` model once compiled.');
this.name = 'OverwriteModelError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
}
/*!
* Inherits from MongooseError.
*/
OverwriteModelError.prototype = Object.create(MongooseError.prototype);
OverwriteModelError.prototype.constructor = MongooseError;
/*!
* exports
*/
module.exports = OverwriteModelError;
+36
View File
@@ -0,0 +1,36 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/**
* Strict mode error constructor
*
* @param {String} type
* @param {String} value
* @inherits MongooseError
* @api private
*/
function StrictModeError(path, msg) {
msg = msg || 'Field `' + path + '` is not in schema and strict ' +
'mode is set to throw.';
MongooseError.call(this, msg);
this.name = 'StrictModeError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
this.path = path;
}
/*!
* Inherits from MongooseError.
*/
StrictModeError.prototype = Object.create(MongooseError.prototype);
StrictModeError.prototype.constructor = MongooseError;
module.exports = StrictModeError;
+102
View File
@@ -0,0 +1,102 @@
/*!
* Module requirements
*/
var MongooseError = require('./');
var utils = require('../utils');
/**
* Document Validation Error
*
* @api private
* @param {Document} instance
* @inherits MongooseError
*/
function ValidationError(instance) {
this.errors = {};
this._message = '';
if (instance && instance.constructor.name === 'model') {
this._message = instance.constructor.modelName + ' validation failed';
MongooseError.call(this, this._message);
} else {
this._message = 'Validation failed';
MongooseError.call(this, this._message);
}
this.name = 'ValidationError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
if (instance) {
instance.errors = this.errors;
}
}
/*!
* Inherits from MongooseError.
*/
ValidationError.prototype = Object.create(MongooseError.prototype);
ValidationError.prototype.constructor = MongooseError;
/**
* Console.log helper
*/
ValidationError.prototype.toString = function() {
return this.name + ': ' + _generateMessage(this);
};
/*!
* inspect helper
*/
ValidationError.prototype.inspect = function() {
return utils.assign(new Error(this.message), this);
};
/*!
* Helper for JSON.stringify
*/
ValidationError.prototype.toJSON = function() {
return utils.assign({}, this, { message: this.message });
};
/*!
* add message
*/
ValidationError.prototype.addError = function(path, error) {
this.errors[path] = error;
this.message = this._message + ': ' + _generateMessage(this);
};
/*!
* ignore
*/
function _generateMessage(err) {
var keys = Object.keys(err.errors || {});
var len = keys.length;
var msgs = [];
var key;
for (var i = 0; i < len; ++i) {
key = keys[i];
if (err === err.errors[key]) {
continue;
}
msgs.push(key + ': ' + err.errors[key].message);
}
return msgs.join(', ');
}
/*!
* Module exports
*/
module.exports = exports = ValidationError;
+82
View File
@@ -0,0 +1,82 @@
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/**
* Schema validator error
*
* @param {Object} properties
* @inherits MongooseError
* @api private
*/
function ValidatorError(properties) {
var msg = properties.message;
if (!msg) {
msg = MongooseError.messages.general.default;
}
var message = this.formatMessage(msg, properties);
MongooseError.call(this, message);
this.name = 'ValidatorError';
if (Error.captureStackTrace) {
Error.captureStackTrace(this);
} else {
this.stack = new Error().stack;
}
this.properties = properties;
this.kind = properties.type;
this.path = properties.path;
this.value = properties.value;
this.reason = properties.reason;
}
/*!
* Inherits from MongooseError
*/
ValidatorError.prototype = Object.create(MongooseError.prototype);
ValidatorError.prototype.constructor = MongooseError;
/*!
* The object used to define this validator. Not enumerable to hide
* it from `require('util').inspect()` output re: gh-3925
*/
Object.defineProperty(ValidatorError.prototype, 'properties', {
enumerable: false,
writable: true,
value: null
});
/*!
* Formats error messages
*/
ValidatorError.prototype.formatMessage = function(msg, properties) {
var propertyNames = Object.keys(properties);
for (var i = 0; i < propertyNames.length; ++i) {
var propertyName = propertyNames[i];
if (propertyName === 'message') {
continue;
}
msg = msg.replace('{' + propertyName.toUpperCase() + '}', properties[propertyName]);
}
return msg;
};
/*!
* toString helper
*/
ValidatorError.prototype.toString = function() {
return this.message;
};
/*!
* exports
*/
module.exports = ValidatorError;
+36
View File
@@ -0,0 +1,36 @@
'use strict';
/*!
* Module dependencies.
*/
var MongooseError = require('./');
/**
* Version Error constructor.
*
* @inherits MongooseError
* @api private
*/
function VersionError(doc, currentVersion, modifiedPaths) {
var modifiedPathsStr = modifiedPaths.join(', ');
MongooseError.call(this, 'No matching document found for id "' + doc._id +
'" version ' + currentVersion + ' modifiedPaths "' + modifiedPathsStr + '"');
this.name = 'VersionError';
this.version = currentVersion;
this.modifiedPaths = modifiedPaths;
}
/*!
* Inherits from MongooseError.
*/
VersionError.prototype = Object.create(MongooseError.prototype);
VersionError.prototype.constructor = MongooseError;
/*!
* exports
*/
module.exports = VersionError;