(err, errStack)
| 222 | } |
| 223 | |
| 224 | formatError(err, errStack) { |
| 225 | switch (err.errno) { |
| 226 | case ER_DUP_ENTRY: { |
| 227 | const match = err.message.match( |
| 228 | /Duplicate entry '([\s\S]*)' for key '?((.|\s)*?)'?\s.*$/); |
| 229 | |
| 230 | let fields = {}; |
| 231 | let message = 'Validation error'; |
| 232 | const values = match ? match[1].split('-') : undefined; |
| 233 | const fieldKey = match ? match[2] : undefined; |
| 234 | const fieldVal = match ? match[1] : undefined; |
| 235 | const uniqueKey = this.model && this.model.uniqueKeys[fieldKey]; |
| 236 | |
| 237 | if (uniqueKey) { |
| 238 | if (uniqueKey.msg) message = uniqueKey.msg; |
| 239 | fields = _.zipObject(uniqueKey.fields, values); |
| 240 | } else { |
| 241 | fields[fieldKey] = fieldVal; |
| 242 | } |
| 243 | |
| 244 | const errors = []; |
| 245 | _.forOwn(fields, (value, field) => { |
| 246 | errors.push(new sequelizeErrors.ValidationErrorItem( |
| 247 | this.getUniqueConstraintErrorMessage(field), |
| 248 | 'unique violation', // sequelizeErrors.ValidationErrorItem.Origins.DB, |
| 249 | field, |
| 250 | value, |
| 251 | this.instance, |
| 252 | 'not_unique' |
| 253 | )); |
| 254 | }); |
| 255 | |
| 256 | return new sequelizeErrors.UniqueConstraintError({ message, errors, parent: err, fields, stack: errStack }); |
| 257 | } |
| 258 | |
| 259 | case ER_ROW_IS_REFERENCED: |
| 260 | case ER_NO_REFERENCED_ROW: { |
| 261 | // e.g. CONSTRAINT `example_constraint_name` FOREIGN KEY (`example_id`) REFERENCES `examples` (`id`) |
| 262 | const match = err.message.match( |
| 263 | /CONSTRAINT ([`"])(.*)\1 FOREIGN KEY \(\1(.*)\1\) REFERENCES \1(.*)\1 \(\1(.*)\1\)/ |
| 264 | ); |
| 265 | const quoteChar = match ? match[1] : '`'; |
| 266 | const fields = match ? match[3].split(new RegExp(`${quoteChar}, *${quoteChar}`)) : undefined; |
| 267 | |
| 268 | return new sequelizeErrors.ForeignKeyConstraintError({ |
| 269 | reltype: err.errno === ER_ROW_IS_REFERENCED ? 'parent' : 'child', |
| 270 | table: match ? match[4] : undefined, |
| 271 | fields, |
| 272 | value: fields && fields.length && this.instance && this.instance[fields[0]] || undefined, |
| 273 | index: match ? match[2] : undefined, |
| 274 | parent: err, |
| 275 | stack: errStack |
| 276 | }); |
| 277 | } |
| 278 | |
| 279 | default: |
| 280 | return new sequelizeErrors.DatabaseError(err, { stack: errStack }); |
| 281 | } |
no test coverage detected