UNPKG

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