index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. 'use strict';
  2. /**
  3. * MongooseError constructor. MongooseError is the base class for all
  4. * Mongoose-specific errors.
  5. *
  6. * #### Example:
  7. * const Model = mongoose.model('Test', new mongoose.Schema({ answer: Number }));
  8. * const doc = new Model({ answer: 'not a number' });
  9. * const err = doc.validateSync();
  10. *
  11. * err instanceof mongoose.Error.ValidationError; // true
  12. *
  13. * @constructor Error
  14. * @param {String} msg Error message
  15. * @inherits Error https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Error
  16. */
  17. const MongooseError = require('./mongooseError');
  18. /**
  19. * The name of the error. The name uniquely identifies this Mongoose error. The
  20. * possible values are:
  21. *
  22. * - `MongooseError`: general Mongoose error
  23. * - `CastError`: Mongoose could not convert a value to the type defined in the schema path. May be in a `ValidationError` class' `errors` property.
  24. * - `DisconnectedError`: This [connection](connections.html) timed out in trying to reconnect to MongoDB and will not successfully reconnect to MongoDB unless you explicitly reconnect.
  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()`](api.html#mongoose_Mongoose-model) that was not defined
  27. * - `DocumentNotFoundError`: The document you tried to [`save()`](api.html#document_Document-save) was not found
  28. * - `ValidatorError`: error from an individual schema path's validator
  29. * - `ValidationError`: error returned from [`validate()`](api.html#document_Document-validate) or [`validateSync()`](api.html#document_Document-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](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()`](api.html#mongoose_Mongoose-model) to re-define a model that was already defined.
  34. * - `ParallelSaveError`: Thrown when you call [`save()`](api.html#model_Model-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](guide.html#strict) is set to `throw`.
  36. * - `VersionError`: Thrown when the [document is out of sync](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 #error_messages_MongooseError-messages
  51. * @api public
  52. * @memberOf Error
  53. * @static messages
  54. */
  55. MongooseError.messages = require('./messages');
  56. // backward compat
  57. MongooseError.Messages = MongooseError.messages;
  58. /**
  59. * An instance of this error class will be returned when `save()` fails
  60. * because the underlying
  61. * document was not found. The constructor takes one parameter, the
  62. * conditions that mongoose passed to `update()` when trying to update
  63. * the document.
  64. *
  65. * @api public
  66. * @memberOf Error
  67. * @static DocumentNotFoundError
  68. */
  69. MongooseError.DocumentNotFoundError = require('./notFound');
  70. /**
  71. * An instance of this error class will be returned when mongoose failed to
  72. * cast a value.
  73. *
  74. * @api public
  75. * @memberOf Error
  76. * @static CastError
  77. */
  78. MongooseError.CastError = require('./cast');
  79. /**
  80. * An instance of this error class will be returned when [validation](/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 ValidationError
  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 ValidatorError
  120. */
  121. MongooseError.ValidatorError = require('./validator');
  122. /**
  123. * An instance of this error class will be returned when you call `save()` after
  124. * the document in the database was changed in a potentially unsafe way. See
  125. * the [`versionKey` option](/docs/guide.html#versionKey) for more information.
  126. *
  127. * @api public
  128. * @memberOf Error
  129. * @static VersionError
  130. */
  131. MongooseError.VersionError = require('./version');
  132. /**
  133. * An instance of this error class will be returned when you call `save()` multiple
  134. * times on the same document in parallel. See the [FAQ](/docs/faq.html) for more
  135. * information.
  136. *
  137. * @api public
  138. * @memberOf Error
  139. * @static ParallelSaveError
  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`](/docs/faq.html#overwrite-model-error).
  145. *
  146. * @api public
  147. * @memberOf Error
  148. * @static OverwriteModelError
  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 MissingSchemaError
  157. */
  158. MongooseError.MissingSchemaError = require('./missingSchema');
  159. /**
  160. * Thrown when the MongoDB Node driver can't connect to a valid server
  161. * to send an operation to.
  162. *
  163. * @api public
  164. * @memberOf Error
  165. * @static MongooseServerSelectionError
  166. */
  167. MongooseError.MongooseServerSelectionError = require('./serverSelection');
  168. /**
  169. * An instance of this error will be returned if you used an array projection
  170. * and then modified the array in an unsafe way.
  171. *
  172. * @api public
  173. * @memberOf Error
  174. * @static DivergentArrayError
  175. */
  176. MongooseError.DivergentArrayError = require('./divergentArray');
  177. /**
  178. * Thrown when your try to pass values to model contrtuctor that
  179. * were not specified in schema or change immutable properties when
  180. * `strict` mode is `"throw"`
  181. *
  182. * @api public
  183. * @memberOf Error
  184. * @static StrictModeError
  185. */
  186. MongooseError.StrictModeError = require('./strict');