Skip to content

Commit

Permalink
Catch stream errors gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Apr 3, 2019
1 parent 772bb83 commit 0382a0b
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions stream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
'use strict'

function apply(cb, input) {
let json
try {
json = JSON.parse(input)
} catch (e) {
process.stderr.write(e.toString() + '\n')
return
}
cb(json)
}

function stream(from, cb) {
let buff = ''
let lastChar = ''
Expand All @@ -15,13 +26,11 @@ function stream(from, cb) {

if (count > 0) {
if (head !== '') {
const json = JSON.parse(head)
cb(json)
apply(cb, head)
head = ''
}

const json = JSON.parse(input)
cb(json)
apply(cb, input)
} else {
head = input
}
Expand Down

0 comments on commit 0382a0b

Please sign in to comment.