不要怂,就是干,撸起袖子干!

Commit 78a1fb6c by Aaron Williams Committed by Sushant

fix(model/bulkCreate): properly pass error instances to AggregateError (#9133)

1 parent d5cd9d8d
......@@ -235,12 +235,16 @@ Tasks.bulkCreate([
[
{ record:
...
name: 'SequelizeBulkRecordError',
message: 'Validation error',
errors:
{ name: 'SequelizeValidationError',
message: 'Validation error',
errors: [Object] } },
{ record:
...
name: 'SequelizeBulkRecordError',
message: 'Validation error',
errors:
{ name: 'SequelizeValidationError',
message: 'Validation error',
......
......@@ -524,3 +524,21 @@ class QueryError extends BaseError {
}
}
exports.QueryError = QueryError;
/**
* Thrown when bulk operation fails, it represent per record level error.
* Used with Promise.AggregateError
*
* @param {Error} error Error for a given record/instance
* @param {Object} record DAO instance that error belongs to
*/
class BulkRecordError extends BaseError {
constructor(error, record) {
super(error.message);
this.name = 'SequelizeBulkRecordError';
this.errors = error;
this.record = record;
Error.captureStackTrace(this, this.constructor);
}
}
exports.BulkRecordError = BulkRecordError;
......@@ -2374,7 +2374,7 @@ class Model {
return Promise.map(instances, instance =>
instance.validate(validateOptions).catch(err => {
errors.push({record: instance, errors: err});
errors.push(new sequelizeErrors.BulkRecordError(err, instance));
})
).then(() => {
delete options.skip;
......
......@@ -292,7 +292,12 @@ describe(Support.getTestDialectTeaser('Model'), () => {
{code: '1234'},
{name: 'bar', code: '1'}
], { validate: true }).catch(errors => {
const expectedValidationError = 'Validation len on code failed';
const expectedNotNullError = 'notNull Violation: Task.name cannot be null';
expect(errors).to.be.instanceof(Promise.AggregateError);
expect(errors.toString()).to.include(expectedValidationError)
.and.to.include(expectedNotNullError);
expect(errors).to.have.length(2);
const e0name0 = errors[0].errors.get('name')[0];
......@@ -302,7 +307,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(errors[1].record.name).to.equal('bar');
expect(errors[1].record.code).to.equal('1');
expect(errors[1].errors.get('code')[0].message).to.equal('Validation len on code failed');
expect(errors[1].errors.get('code')[0].message).to.equal(expectedValidationError);
});
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!