Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handling mapper throwing exception #39

Merged
merged 2 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ function transform (chunk, enc, cb) {
this[kLast] = list.pop()

for (var i = 0; i < list.length; i++) {
push(this, this.mapper(list[i]))
try {
push(this, this.mapper(list[i]))
} catch (error) {
return cb(error)
}
}

this.overflow = this[kLast].length > this.maxLength
Expand All @@ -54,7 +58,11 @@ function flush (cb) {
this[kLast] += this[kDecoder].end()

if (this[kLast]) {
push(this, this.mapper(this[kLast]))
try {
push(this, this.mapper(this[kLast]))
} catch (error) {
return cb(error)
}
}

cb()
Expand Down
34 changes: 32 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ test('splits a buffer', function (t) {
test('do not end on undefined', function (t) {
t.plan(2)

var input = split(function (line) {})
var input = split(function (line) { })

input.pipe(strcb(function (err, list) {
t.error(err)
Expand All @@ -165,7 +165,7 @@ test('do not end on undefined', function (t) {
test('has destroy method', function (t) {
t.plan(1)

var input = split(function (line) {})
var input = split(function (line) { })

input.on('close', function () {
t.ok(true, 'close emitted')
Expand Down Expand Up @@ -361,3 +361,33 @@ test("don't modify the options object", function (t) {

input.end()
})

test('mapper throws flush', function (t) {
t.plan(1)
var error = new Error()
var input = split(function () {
throw error
})

input.on('error', (err, list) => {
t.same(err, error)
})
input.end('hello')
})

test('mapper throws on transform', function (t) {
t.plan(2)

var error = new Error()
var input = split(function (l) {
console.log(l)
razaba marked this conversation as resolved.
Show resolved Hide resolved
throw error
})

input.on('error', (err) => {
t.same(err, error)
})
input.write('a')
input.write('\n')
input.end('b')
})