Skip to content

Instantly share code, notes, and snippets.

@gyaresu
Created May 23, 2015 22:23
Show Gist options
  • Save gyaresu/8ec65881ef162bf879ec to your computer and use it in GitHub Desktop.
Save gyaresu/8ec65881ef162bf879ec to your computer and use it in GitHub Desktop.
How to catch a stream error (using through2)
var tr = require('through2')
function write(buffer, encoding, next) {
try {
this.push(buffer.toUpperCase()) //buffer needs .toString()
} catch (e) {
this.emit('error', e)
}
next()
}
function end(done) {
done()
}
var stream = tr(write, end)
stream.on('error', function (e) {
console.log('Broked it.', e)
})
process.stdin.pipe(stream).pipe(process.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment