MCPcopy
hub / github.com/sequelize/sequelize / formatError

Method formatError

lib/dialects/mssql/query.js:259–325  ·  view source on GitHub ↗
(err, errStack)

Source from the content-addressed store, hash-verified

257 }
258
259 formatError(err, errStack) {
260 let match;
261
262 match = err.message.match(/Violation of (?:UNIQUE|PRIMARY) KEY constraint '([^']*)'. Cannot insert duplicate key in object '.*'.(:? The duplicate key value is \((.*)\).)?/);
263 match = match || err.message.match(/Cannot insert duplicate key row in object .* with unique index '(.*)'/);
264 if (match && match.length > 1) {
265 let fields = {};
266 const uniqueKey = this.model && this.model.uniqueKeys[match[1]];
267 let message = 'Validation error';
268
269 if (uniqueKey && !!uniqueKey.msg) {
270 message = uniqueKey.msg;
271 }
272 if (match[3]) {
273 const values = match[3].split(',').map(part => part.trim());
274 if (uniqueKey) {
275 fields = _.zipObject(uniqueKey.fields, values);
276 } else {
277 fields[match[1]] = match[3];
278 }
279 }
280
281 const errors = [];
282 _.forOwn(fields, (value, field) => {
283 errors.push(new sequelizeErrors.ValidationErrorItem(
284 this.getUniqueConstraintErrorMessage(field),
285 'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB,
286 field,
287 value,
288 this.instance,
289 'not_unique'
290 ));
291 });
292
293 return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields, stack: errStack });
294 }
295
296 match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./) ||
297 err.message.match(/The DELETE statement conflicted with the REFERENCE constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./) ||
298 err.message.match(/The (?:INSERT|MERGE|UPDATE) statement conflicted with the FOREIGN KEY constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./);
299 if (match && match.length > 0) {
300 return new sequelizeErrors.ForeignKeyConstraintError({
301 fields: null,
302 index: match[1],
303 parent: err,
304 stack: errStack
305 });
306 }
307
308 match = err.message.match(/Could not drop constraint. See previous errors./);
309 if (match && match.length > 0) {
310 let constraint = err.sql.match(/(?:constraint|index) \[(.+?)\]/i);
311 constraint = constraint ? constraint[1] : undefined;
312 let table = err.sql.match(/table \[(.+?)\]/i);
313 table = table ? table[1] : undefined;
314
315 return new sequelizeErrors.UnknownConstraintError({
316 message: match[1],

Callers 1

_runMethod · 0.95

Calls 1

Tested by

no test coverage detected