Skip to content

Commit

Permalink
Fix error message bubbling up on seed error (#3248)
Browse files Browse the repository at this point in the history
  • Loading branch information
risseraka authored and kibertoad committed Jun 3, 2019
1 parent a2031e8 commit 9b74ccb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
39 changes: 20 additions & 19 deletions src/seed/Seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,26 @@ Seeder.prototype._waterfallBatch = function(seeds) {
seed = require(name);

// Run each seed file.
current = current
.then(() => seed.seed(knex, Promise))
.then(() => {
log.push(name);
})
.catch((originalError) => {
const error = new Error(
`Error while executing "${name}" seed: ${originalError.message}`
);
error.original = originalError;
error.stack =
error.stack
.split('\n')
.slice(0, 2)
.join('\n') +
'\n' +
originalError.stack;
throw error;
});
current = current.then(() =>
// Nesting promise to prevent bubbling up of error on catch
Promise.resolve()
.then(() => seed.seed(knex, Promise))
.then(() => log.push(name))
.catch((originalError) => {
const error = new Error(
`Error while executing "${name}" seed: ${originalError.message}`
);
error.original = originalError;
error.stack =
error.stack
.split('\n')
.slice(0, 2)
.join('\n') +
'\n' +
originalError.stack;
throw error;
})
);
});

return current.thenReturn([log]);
Expand Down
31 changes: 30 additions & 1 deletion test/unit/seed/seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var knex = require('../../../knex');

describe('Seeder.loadExtensions', function() {
var config = {
client: 'pg',
client: 'postgres',
connection: {
user: 'postgres',
password: '',
Expand Down Expand Up @@ -67,3 +67,32 @@ describe('Seeder.loadExtensions', function() {
});
});
});

describe('Seeder._waterfallBatch', function() {
var config = {
client: 'postgres',
connection: {
user: 'postgres',
password: '',
host: '127.0.0.1',
database: 'knex_test',
},
seeds: {
directory: 'test/unit/seed/test',
},
};
var seeder;

beforeEach(function() {
seeder = knex(config).seed;
});

it('should throw an error with correct file name', (done) => {
seeder._waterfallBatch(['1-first.js', '2-second.js']).catch((error) => {
expect(error.message).to.match(
/^Error while executing "(\/\w+)+\/1-first\.js" seed: throwing in first file$/
);
done();
});
});
});
5 changes: 5 additions & 0 deletions test/unit/seed/test/1-first.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.seed = function() {
throw new Error('throwing in first file');
};
5 changes: 5 additions & 0 deletions test/unit/seed/test/2-second.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

exports.seed = function() {
return 'doing nothing in second file';
};

0 comments on commit 9b74ccb

Please sign in to comment.