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