From e193425549bc3e4e3da6a3b3d9a3e35deec676b3 Mon Sep 17 00:00:00 2001 From: Steve Mao Date: Sat, 6 Feb 2016 08:32:16 +1100 Subject: [PATCH] any error thrown by mapper should be emitted Ref: https://github.com/dominictarr/split/blob/master/index.js#L27-L32 --- index.js | 9 +++++++-- test.js | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 99c6e14..2b03a19 100644 --- a/index.js +++ b/index.js @@ -27,7 +27,7 @@ function transform (chunk, enc, cb) { this._last = list.pop() for (var i = 0; i < list.length; i++) { - push(this, this.mapper(list[i])) + push(this, list[i]) } cb() @@ -38,13 +38,18 @@ function flush (cb) { this._last += this._decoder.end() if (this._last) { - push(this, this.mapper(this._last)) + push(this, this._last) } cb() } function push (self, val) { + try { + val = self.mapper(val) + } catch (err) { + return self.emit('error', err) + } if (val !== undefined) { self.push(val) } diff --git a/test.js b/test.js index 2cd3522..ad25b82 100644 --- a/test.js +++ b/test.js @@ -218,6 +218,21 @@ test('support mapper and options', function (t) { input.end(JSON.stringify(b)) }) +test('emit error if mapper throws', function (t) { + t.plan(1) + + var a = { a: '42' } + var b = { b: '24' } + var input = split(JSON.parse) + + input.on('error', function (err) { + t.ok(err) + }) + + input.write(JSON.stringify(a)) + input.end(JSON.stringify(b)) +}) + test('split utf8 chars', function (t) { t.plan(2)