Skip to content

Instantly share code, notes, and snippets.

@jgornick
Last active August 29, 2015 14:16
Show Gist options
  • Save jgornick/cd2b8bf0d8ba76b5d747 to your computer and use it in GitHub Desktop.
Save jgornick/cd2b8bf0d8ba76b5d747 to your computer and use it in GitHub Desktop.
fast-csv Stream Error Issues
// With this example, the error that is thrown outside of the stream is actually caught inside the stream.on('error') handler.
var
fs = require('fs'),
csv = require('fast-csv');
function throwError() {
throw new Error('implicit error');
}
fs
.createReadStream('/tmp/file.csv')
.pipe(csv())
.on('error', function(error) {
console.log('stream error', error);
})
.on('data', function(data) {
console.log('stream data', data);
})
.on('end', function() {
console.log('stream end');
throwError();
});
// With this example, the thrown error is handled at the process level and not the stream. This tells me that there is something wrong with how fast-csv handles it's event and in particular the error event.
var
stream = require('stream'),
fs = require('fs'),
csv = require('fast-csv');
function throwError() {
throw new Error('implicit error');
}
fs
.createReadStream('/tmp/file.csv')
.pipe(csv({ headers: true, objectMode: false }))
.pipe(new stream.PassThrough())
.on('error', function(error) {
console.log('stream error', error);
})
.on('data', function(data) {
console.log('stream data', JSON.parse(data.toString('utf8')));
})
.on('end', function() {
console.log('stream end');
throwError();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment