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

Method formatError

lib/dialects/db2/query.js:336–421  ·  view source on GitHub ↗
(err, conn, parameters)

Source from the content-addressed store, hash-verified

334 }
335
336 formatError(err, conn, parameters) {
337 let match;
338
339 if (!(err && err.message)) {
340 err['message'] = 'No error message found.';
341 }
342
343 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./);
344 if (match && match.length > 0) {
345 let uniqueIndexName = '';
346 let uniqueKey = '';
347 const fields = {};
348 let message = err.message;
349 const query = `SELECT INDNAME FROM SYSCAT.INDEXES WHERE IID = ${match[1]} AND TABSCHEMA = '${match[2]}' AND TABNAME = '${match[3]}'`;
350
351 if (!!conn && match.length > 3) {
352 uniqueIndexName = conn.querySync(query);
353 uniqueIndexName = uniqueIndexName[0]['INDNAME'];
354 }
355
356 if (this.model && !!uniqueIndexName) {
357 uniqueKey = this.model.uniqueKeys[uniqueIndexName];
358 }
359
360 if (!uniqueKey && this.options.fields) {
361 uniqueKey = this.options.fields[match[1] - 1];
362 }
363
364 if (uniqueKey) {
365 if (this.options.where &&
366 this.options.where[uniqueKey.column] !== undefined) {
367 fields[uniqueKey.column] = this.options.where[uniqueKey.column];
368 } else if (this.options.instance && this.options.instance.dataValues &&
369 this.options.instance.dataValues[uniqueKey.column]) {
370 fields[uniqueKey.column] = this.options.instance.dataValues[uniqueKey.column];
371 } else if (parameters) {
372 fields[uniqueKey.column] = parameters['0'];
373 }
374 }
375
376 if (uniqueKey && !!uniqueKey.msg) {
377 message = uniqueKey.msg;
378 }
379
380 const errors = [];
381 _.forOwn(fields, (value, field) => {
382 errors.push(new sequelizeErrors.ValidationErrorItem(
383 this.getUniqueConstraintErrorMessage(field),
384 'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB,
385 field,
386 value,
387 this.instance,
388 'not_unique'
389 ));
390 });
391
392 return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields });
393 }

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