MCPcopy Index your code
hub / github.com/sequelize/sequelize / formatError

Method formatError

src/dialects/db2/query.js:348–435  ·  view source on GitHub ↗
(err, errStack, conn, parameters)

Source from the content-addressed store, hash-verified

346 }
347
348 formatError(err, errStack, conn, parameters) {
349 let match;
350
351 if (!(err && err.message)) {
352 err['message'] = 'No error message found.';
353 }
354
355 match = err.message.match(/SQL0803N {2}One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "(\d)+" constrains table "(.*)\.(.*)" from having duplicate values for the index key./);
356 if (match && match.length > 0) {
357 let uniqueIndexName = '';
358 let uniqueKey = '';
359 const fields = {};
360 let message = err.message;
361 const query = `SELECT INDNAME FROM SYSCAT.INDEXES WHERE IID = ${match[1]} AND TABSCHEMA = '${match[2]}' AND TABNAME = '${match[3]}'`;
362
363 if (!!conn && match.length > 3) {
364 uniqueIndexName = conn.querySync(query);
365 uniqueIndexName = uniqueIndexName[0]['INDNAME'];
366 }
367
368 if (this.model && !!uniqueIndexName) {
369 uniqueKey = this.model.uniqueKeys[uniqueIndexName];
370 }
371
372 if (!uniqueKey && this.options.fields) {
373 uniqueKey = this.options.fields[match[1] - 1];
374 }
375
376 if (uniqueKey) {
377 if (this.options.where &&
378 this.options.where[uniqueKey.column] !== undefined) {
379 fields[uniqueKey.column] = this.options.where[uniqueKey.column];
380 } else if (this.options.instance && this.options.instance.dataValues &&
381 this.options.instance.dataValues[uniqueKey.column]) {
382 fields[uniqueKey.column] = this.options.instance.dataValues[uniqueKey.column];
383 } else if (parameters) {
384 fields[uniqueKey.column] = parameters['0'];
385 }
386 }
387
388 if (uniqueKey && !!uniqueKey.msg) {
389 message = uniqueKey.msg;
390 }
391
392 const errors = [];
393 _.forOwn(fields, (value, field) => {
394 errors.push(new sequelizeErrors.ValidationErrorItem(
395 this.getUniqueConstraintErrorMessage(field),
396 'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB,
397 field,
398 value,
399 this.instance,
400 'not_unique'
401 ));
402 });
403
404 return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields, stack: errStack });
405 }

Callers 4

_runMethod · 0.95
errors.test.jsFile · 0.45
errors.test.jsFile · 0.45
errors.test.jsFile · 0.45

Calls 1

Tested by

no test coverage detected