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

Method formatError

src/dialects/sqlite/query.js:370–431  ·  view source on GitHub ↗
(err, errStack)

Source from the content-addressed store, hash-verified

368 }
369
370 formatError(err, errStack) {
371
372 switch (err.code) {
373 case 'SQLITE_CONSTRAINT_UNIQUE':
374 case 'SQLITE_CONSTRAINT_PRIMARYKEY':
375 case 'SQLITE_CONSTRAINT_TRIGGER':
376 case 'SQLITE_CONSTRAINT_FOREIGNKEY':
377 case 'SQLITE_CONSTRAINT': {
378 if (err.message.includes('FOREIGN KEY constraint failed')) {
379 return new sequelizeErrors.ForeignKeyConstraintError({
380 parent: err,
381 stack: errStack
382 });
383 }
384
385 let fields = [];
386
387 // Sqlite pre 2.2 behavior - Error: SQLITE_CONSTRAINT: columns x, y are not unique
388 let match = err.message.match(/columns (.*?) are/);
389 if (match !== null && match.length >= 2) {
390 fields = match[1].split(', ');
391 } else {
392
393 // Sqlite post 2.2 behavior - Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: table.x, table.y
394 match = err.message.match(/UNIQUE constraint failed: (.*)/);
395 if (match !== null && match.length >= 2) {
396 fields = match[1].split(', ').map(columnWithTable => columnWithTable.split('.')[1]);
397 }
398 }
399
400 const errors = [];
401 let message = 'Validation error';
402
403 for (const field of fields) {
404 errors.push(new sequelizeErrors.ValidationErrorItem(
405 this.getUniqueConstraintErrorMessage(field),
406 'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB,
407 field,
408 this.instance && this.instance[field],
409 this.instance,
410 'not_unique'
411 ));
412 }
413
414 if (this.model) {
415 _.forOwn(this.model.uniqueKeys, constraint => {
416 if (_.isEqual(constraint.fields, fields) && !!constraint.msg) {
417 message = constraint.msg;
418 return false;
419 }
420 });
421 }
422
423 return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields, stack: errStack });
424 }
425 case 'SQLITE_BUSY':
426 return new sequelizeErrors.TimeoutError(err, { stack: errStack });
427

Callers 1

_handleQueryResponseMethod · 0.95

Calls 1

Tested by

no test coverage detected