(err, errStack)
| 266 | } |
| 267 | |
| 268 | formatError(err, errStack) { |
| 269 | let match; |
| 270 | |
| 271 | match = err.message.match(/Violation of (?:UNIQUE|PRIMARY) KEY constraint '([^']*)'. Cannot insert duplicate key in object '.*'.(:? The duplicate key value is \((.*)\).)?/); |
| 272 | match = match || err.message.match(/Cannot insert duplicate key row in object .* with unique index '(.*)'/); |
| 273 | if (match && match.length > 1) { |
| 274 | let fields = {}; |
| 275 | const uniqueKey = this.model && this.model.uniqueKeys[match[1]]; |
| 276 | let message = 'Validation error'; |
| 277 | |
| 278 | if (uniqueKey && !!uniqueKey.msg) { |
| 279 | message = uniqueKey.msg; |
| 280 | } |
| 281 | if (match[3]) { |
| 282 | const values = match[3].split(',').map(part => part.trim()); |
| 283 | if (uniqueKey) { |
| 284 | fields = _.zipObject(uniqueKey.fields, values); |
| 285 | } else { |
| 286 | fields[match[1]] = match[3]; |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | const errors = []; |
| 291 | _.forOwn(fields, (value, field) => { |
| 292 | errors.push(new sequelizeErrors.ValidationErrorItem( |
| 293 | this.getUniqueConstraintErrorMessage(field), |
| 294 | 'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB, |
| 295 | field, |
| 296 | value, |
| 297 | this.instance, |
| 298 | 'not_unique' |
| 299 | )); |
| 300 | }); |
| 301 | |
| 302 | return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields, stack: errStack }); |
| 303 | } |
| 304 | |
| 305 | match = err.message.match(/Failed on step '(.*)'.Could not create constraint. See previous errors./) || |
| 306 | err.message.match(/The DELETE statement conflicted with the REFERENCE constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./) || |
| 307 | err.message.match(/The (?:INSERT|MERGE|UPDATE) statement conflicted with the FOREIGN KEY constraint "(.*)". The conflict occurred in database "(.*)", table "(.*)", column '(.*)'./); |
| 308 | if (match && match.length > 0) { |
| 309 | return new sequelizeErrors.ForeignKeyConstraintError({ |
| 310 | fields: null, |
| 311 | index: match[1], |
| 312 | parent: err, |
| 313 | stack: errStack |
| 314 | }); |
| 315 | } |
| 316 | |
| 317 | match = err.message.match(/Could not drop constraint. See previous errors./); |
| 318 | if (match && match.length > 0) { |
| 319 | let constraint = err.sql.match(/(?:constraint|index) \[(.+?)\]/i); |
| 320 | constraint = constraint ? constraint[1] : undefined; |
| 321 | let table = err.sql.match(/table \[(.+?)\]/i); |
| 322 | table = table ? table[1] : undefined; |
| 323 | |
| 324 | return new sequelizeErrors.UnknownConstraintError({ |
| 325 | message: match[1], |
no test coverage detected