mafintosh / csv-parser

Streaming csv parser inspired by binary-csv that aims to be faster than everyone else
MIT License
1.41k stars 134 forks source link

Error handling #146

Closed iczn closed 4 years ago

iczn commented 4 years ago

When using the fs reading mechanism with pipes, an error is thrown in strict mode if one of the rows does not match the length of the header. However, a try-catch block around the entire chain won't catch the error. Here's an example:

try {
    fs.createReadStream(file_path)
    .pipe(csv({
        separator: ",",
        strict: true
    }))
    .on('headers', function (headers) {
        console.log("headers: ", headers)
    })
    .on('data', function (data) {
        // a row was parsed
        results.push(data)
    })
    .on('end', function () {
        //
        console.log(results);
    });
} catch (err) {
    // error parsing csv - it never gets here
}
shellscape commented 4 years ago

The issues here aren't for support. Please read about error handling with streams, which is what you're using when you pipe.