index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. 'use strict';
  2. /**
  3. * MongooseError constructor. MongooseError is the base class for all
  4. * Mongoose-specific errors.
  5. *
  6. * #### Example:
  7. *
  8. * const Model = mongoose.model('Test', new mongoose.Schema({ answer: Number }));
  9. * const doc = new Model({ answer: 'not a number' });
  10. * const err = doc.validateSync();
  11. *
  12. * err instanceof mongoose.Error.ValidationError; // true
  13. *
  14. * @constructor Error
  15. * @param {String} msg Error message
  16. * @inherits Error https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error
  17. */
  18. const MongooseError = require('./mongooseError');
  19. /**
  20. * The name of the error. The name uniquely identifies this Mongoose error. The
  21. * possible values are:
  22. *
  23. * - `MongooseError`: general Mongoose error
  24. * - `CastError`: Mongoose could not convert a value to the type defined in the schema path. May be in a `ValidationError` class' `errors` property.
  25. * - `DivergentArrayError`: You attempted to `save()` an array that was modified after you loaded it with a `$elemMatch` or similar projection
  26. * - `MissingSchemaError`: You tried to access a model with [`mongoose.model()`](https://mongoosejs.com/docs/api/mongoose.html#Mongoose.model()) that was not defined
  27. * - `DocumentNotFoundError`: The document you tried to [`save()`](https://mongoosejs.com/docs/api/document.html#Document.prototype.save()) was not found
  28. * - `ValidatorError`: error from an individual schema path's validator
  29. * - `ValidationError`: error returned from [`validate()`](https://mongoosejs.com/docs/api/document.html#Document.prototype.validate()) or [`validateSync()`](https://mongoosejs.com/docs/api/document.html#Document.prototype.validateSync()). Contains zero or more `ValidatorError` instances in `.errors` property.
  30. * - `MissingSchemaError`: You called `mongoose.Document()` without a schema
  31. * - `ObjectExpectedError`: Thrown when you set a nested path to a non-object value with [strict mode set](https://mongoosejs.com/docs/guide.html#strict).
  32. * - `ObjectParameterError`: Thrown when you pass a non-object value to a function which expects an object as a paramter
  33. * - `OverwriteModelError`: Thrown when you call [`mongoose.model()`](https://mongoosejs.com/docs/api/mongoose.html#Mongoose.model()) to re-define a model that was already defined.
  34. * - `ParallelSaveError`: Thrown when you call [`save()`](https://mongoosejs.com/docs/api/model.html#Model.prototype.save()) on a document when the same document instance is already saving.
  35. * - `StrictModeError`: Thrown when you set a path that isn't the schema and [strict mode](https://mongoosejs.com/docs/guide.html#strict) is set to `throw`.
  36. * - `VersionError`: Thrown when the [document is out of sync](https://mongoosejs.com/docs/guide.html#versionKey)
  37. *
  38. * @api public
  39. * @property {String} name
  40. * @memberOf Error
  41. * @instance
  42. */
  43. /*!
  44. * Module exports.
  45. */
  46. module.exports = exports = MongooseError;
  47. /**
  48. * The default built-in validator error messages.
  49. *
  50. * @see Error.messages https://mongoosejs.com/docs/api/error.html#Error.messages
  51. * @api public
  52. * @memberOf Error
  53. * @static
  54. */
  55. MongooseError.messages = require('./messages');
  56. // backward compat
  57. MongooseError.Messages = MongooseError.messages;
  58. /**
  59. * An instance of this error class will be thrown when mongoose failed to
  60. * cast a value.
  61. *
  62. * @api public
  63. * @memberOf Error
  64. * @static
  65. */
  66. MongooseError.CastError = require('./cast');
  67. /**
  68. * An instance of this error class will be thrown when `save()` fails
  69. * because the underlying
  70. * document was not found. The constructor takes one parameter, the
  71. * conditions that mongoose passed to `updateOne()` when trying to update
  72. * the document.
  73. *
  74. * @api public
  75. * @memberOf Error
  76. * @static
  77. */
  78. MongooseError.DocumentNotFoundError = require('./notFound');
  79. /**
  80. * An instance of this error class will be thrown when [validation](https://mongoosejs.com/docs/validation.html) failed.
  81. * The `errors` property contains an object whose keys are the paths that failed and whose values are
  82. * instances of CastError or ValidationError.
  83. *
  84. * @api public
  85. * @memberOf Error
  86. * @static
  87. */
  88. MongooseError.ValidationError = require('./validation');
  89. /**
  90. * A `ValidationError` has a hash of `errors` that contain individual
  91. * `ValidatorError` instances.
  92. *
  93. * #### Example:
  94. *
  95. * const schema = Schema({ name: { type: String, required: true } });
  96. * const Model = mongoose.model('Test', schema);
  97. * const doc = new Model({});
  98. *
  99. * // Top-level error is a ValidationError, **not** a ValidatorError
  100. * const err = doc.validateSync();
  101. * err instanceof mongoose.Error.ValidationError; // true
  102. *
  103. * // A ValidationError `err` has 0 or more ValidatorErrors keyed by the
  104. * // path in the `err.errors` property.
  105. * err.errors['name'] instanceof mongoose.Error.ValidatorError;
  106. *
  107. * err.errors['name'].kind; // 'required'
  108. * err.errors['name'].path; // 'name'
  109. * err.errors['name'].value; // undefined
  110. *
  111. * Instances of `ValidatorError` have the following properties:
  112. *
  113. * - `kind`: The validator's `type`, like `'required'` or `'regexp'`
  114. * - `path`: The path that failed validation
  115. * - `value`: The value that failed validation
  116. *
  117. * @api public
  118. * @memberOf Error
  119. * @static
  120. */
  121. MongooseError.ValidatorError = require('./validator');
  122. /**
  123. * An instance of this error class will be thrown when you call `save()` after
  124. * the document in the database was changed in a potentially unsafe way. See
  125. * the [`versionKey` option](https://mongoosejs.com/docs/guide.html#versionKey) for more information.
  126. *
  127. * @api public
  128. * @memberOf Error
  129. * @static
  130. */
  131. MongooseError.VersionError = require('./version');
  132. /**
  133. * An instance of this error class will be thrown when you call `save()` multiple
  134. * times on the same document in parallel. See the [FAQ](https://mongoosejs.com/docs/faq.html) for more
  135. * information.
  136. *
  137. * @api public
  138. * @memberOf Error
  139. * @static
  140. */
  141. MongooseError.ParallelSaveError = require('./parallelSave');
  142. /**
  143. * Thrown when a model with the given name was already registered on the connection.
  144. * See [the FAQ about `OverwriteModelError`](https://mongoosejs.com/docs/faq.html#overwrite-model-error).
  145. *
  146. * @api public
  147. * @memberOf Error
  148. * @static
  149. */
  150. MongooseError.OverwriteModelError = require('./overwriteModel');
  151. /**
  152. * Thrown when you try to access a model that has not been registered yet
  153. *
  154. * @api public
  155. * @memberOf Error
  156. * @static
  157. */
  158. MongooseError.MissingSchemaError = require('./missingSchema');
  159. /**
  160. * Thrown when some documents failed to save when calling `bulkSave()`
  161. *
  162. * @api public
  163. * @memberOf Error
  164. * @static
  165. */
  166. MongooseError.MongooseBulkSaveIncompleteError = require('./bulkSaveIncompleteError');
  167. /**
  168. * Thrown when the MongoDB Node driver can't connect to a valid server
  169. * to send an operation to.
  170. *
  171. * @api public
  172. * @memberOf Error
  173. * @static
  174. */
  175. MongooseError.MongooseServerSelectionError = require('./serverSelection');
  176. /**
  177. * An instance of this error will be thrown if you used an array projection
  178. * and then modified the array in an unsafe way.
  179. *
  180. * @api public
  181. * @memberOf Error
  182. * @static
  183. */
  184. MongooseError.DivergentArrayError = require('./divergentArray');
  185. /**
  186. * Thrown when your try to pass values to model constructor that
  187. * were not specified in schema or change immutable properties when
  188. * `strict` mode is `"throw"`
  189. *
  190. * @api public
  191. * @memberOf Error
  192. * @static
  193. */
  194. MongooseError.StrictModeError = require('./strict');
  195. /**
  196. * An instance of this error class will be returned when mongoose failed to
  197. * populate with a path that is not existing.
  198. *
  199. * @api public
  200. * @memberOf Error
  201. * @static
  202. */
  203. MongooseError.StrictPopulateError = require('./strictPopulate');