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

Method formatError

src/dialects/postgres/query.js:311–413  ·  view source on GitHub ↗
(err, errStack)

Source from the content-addressed store, hash-verified

309 }
310
311 formatError(err, errStack) {
312 let match;
313 let table;
314 let index;
315 let fields;
316 let errors;
317 let message;
318
319 const code = err.code || err.sqlState;
320 const errMessage = err.message || err.messagePrimary;
321 const errDetail = err.detail || err.messageDetail;
322
323 switch (code) {
324 case '23503':
325 index = errMessage.match(/violates foreign key constraint "(.+?)"/);
326 index = index ? index[1] : undefined;
327 table = errMessage.match(/on table "(.+?)"/);
328 table = table ? table[1] : undefined;
329
330 return new sequelizeErrors.ForeignKeyConstraintError({
331 message: errMessage,
332 fields: null,
333 index,
334 table,
335 parent: err,
336 stack: errStack
337 });
338 case '23505':
339 // there are multiple different formats of error messages for this error code
340 // this regex should check at least two
341 if (errDetail && (match = errDetail.replace(/"/g, '').match(/Key \((.*?)\)=\((.*?)\)/))) {
342 fields = _.zipObject(match[1].split(', '), match[2].split(', '));
343 errors = [];
344 message = 'Validation error';
345
346 _.forOwn(fields, (value, field) => {
347 errors.push(new sequelizeErrors.ValidationErrorItem(
348 this.getUniqueConstraintErrorMessage(field),
349 'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB,
350 field,
351 value,
352 this.instance,
353 'not_unique'
354 ));
355 });
356
357 if (this.model && this.model.uniqueKeys) {
358 _.forOwn(this.model.uniqueKeys, constraint => {
359 if (_.isEqual(constraint.fields, Object.keys(fields)) && !!constraint.msg) {
360 message = constraint.msg;
361 return false;
362 }
363 });
364 }
365
366 return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields, stack: errStack });
367 }
368

Callers 1

runMethod · 0.95

Calls 1

Tested by

no test coverage detected