Skip to content
Permalink
Browse files

Catch stream errors gracefully

  • Loading branch information...
antonmedv committed Apr 3, 2019
1 parent 772bb83 commit 0382a0b2d72495fc89a6196e4eb4afd49aa513a0
Showing with 13 additions and 4 deletions.
  1. +13 −4 stream.js
@@ -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 = ''
@@ -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
}

0 comments on commit 0382a0b

Please sign in to comment.
You can’t perform that action at this time.