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

Method formatError

lib/dialects/postgres/query.js:303–405  ·  lib/dialects/postgres/query.js::Query.formatError
(err, errStack)

Source from the content-addressed store, hash-verified

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

Callers 1

runMethod · 0.95

Calls 1

Tested by

no test coverage detected