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

Method formatError

lib/dialects/sqlite/query.js:350–411  ·  view source on GitHub ↗
(err, errStack)

Source from the content-addressed store, hash-verified

348 }
349
350 formatError(err, errStack) {
351
352 switch (err.code) {
353 case 'SQLITE_CONSTRAINT_UNIQUE':
354 case 'SQLITE_CONSTRAINT_PRIMARYKEY':
355 case 'SQLITE_CONSTRAINT_TRIGGER':
356 case 'SQLITE_CONSTRAINT_FOREIGNKEY':
357 case 'SQLITE_CONSTRAINT': {
358 if (err.message.includes('FOREIGN KEY constraint failed')) {
359 return new sequelizeErrors.ForeignKeyConstraintError({
360 parent: err,
361 stack: errStack
362 });
363 }
364
365 let fields = [];
366
367 // Sqlite pre 2.2 behavior - Error: SQLITE_CONSTRAINT: columns x, y are not unique
368 let match = err.message.match(/columns (.*?) are/);
369 if (match !== null && match.length >= 2) {
370 fields = match[1].split(', ');
371 } else {
372
373 // Sqlite post 2.2 behavior - Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: table.x, table.y
374 match = err.message.match(/UNIQUE constraint failed: (.*)/);
375 if (match !== null && match.length >= 2) {
376 fields = match[1].split(', ').map(columnWithTable => columnWithTable.split('.')[1]);
377 }
378 }
379
380 const errors = [];
381 let message = 'Validation error';
382
383 for (const field of fields) {
384 errors.push(new sequelizeErrors.ValidationErrorItem(
385 this.getUniqueConstraintErrorMessage(field),
386 'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB,
387 field,
388 this.instance && this.instance[field],
389 this.instance,
390 'not_unique'
391 ));
392 }
393
394 if (this.model) {
395 _.forOwn(this.model.uniqueKeys, constraint => {
396 if (_.isEqual(constraint.fields, fields) && !!constraint.msg) {
397 message = constraint.msg;
398 return false;
399 }
400 });
401 }
402
403 return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields, stack: errStack });
404 }
405 case 'SQLITE_BUSY':
406 return new sequelizeErrors.TimeoutError(err, { stack: errStack });
407

Callers 1

_handleQueryResponseMethod · 0.95

Calls 1

Tested by

no test coverage detected