Skip to content

Commit dd3b245

Browse files
committed
Emit errors on bulk and finish event instead of end
1 parent fc2390a commit dd3b245

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

lib/writable-bulk.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ WritableBulk.prototype._write = function(chunk, enc, next) {
6363
}
6464
if (!this.expectingPayload) {
6565
if (!chunk.hasOwnProperty('delete')) {
66+
console.log('humf', chunk);
6667
this.emit('error', new Error('Unexpected chunk, not an ' +
6768
'index/create/update/delete command and ' +
6869
'not a document to index either'));
@@ -83,13 +84,19 @@ WritableBulk.prototype._flushBulk = function(callback) {
8384
return setImmediate(callback);
8485
}
8586
var self = this;
86-
this.bulkExec(this.bulk, function(e) {
87+
this.bulkExec(this.bulk, function(e, resp) {
8788
if (e) {
88-
// TODO: better than this?
89-
// - Introspect the response for individual errors
90-
// - Stream out the responses and correlate with the inputs?
9189
self.emit('error', e);
9290
}
91+
if (resp.errors && resp.items) {
92+
for (var i = 0; i < resp.items.length; i++) {
93+
var bulkItemResp = resp.items[i];
94+
var key = Object.keys(bulkItemResp)[0];
95+
if (bulkItemResp[key].error) {
96+
self.emit('error', new Error(bulkItemResp[key].error));
97+
}
98+
}
99+
}
93100
self.bulk = [];
94101
self.bulkCount = 0;
95102
self.expectingPayload = false;
@@ -101,12 +108,12 @@ WritableBulk.prototype.end = function(data) {
101108
var self = this;
102109
if (!data) {
103110
return this._flushBulk(function() {
104-
self.emit('end');
111+
self.emit('finish');
105112
});
106113
}
107114
this._write(data, 'json', function() {
108115
self._flushBulk(function() {
109-
self.emit('end');
116+
self.emit('finish');
110117
});
111118
});
112119
};

test/test-write.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ describe('When writing', function() {
2020
ws = new WritableBulk(bulkExec);
2121
ws.on('error', function(e) {
2222
err = e;
23-
});
24-
ws.on('end', function() {
23+
}).on('finish', function() {
2524
done(err);
2625
});
2726
// drop the index then

0 commit comments

Comments
 (0)