Error handling within on_record callback #275
Description
Describe the bug
Version: 4.8.5
Errors throwing within the on_record callback are not effected by the skip_lines_with_error
option, this is making it difficult for me to utilise the on_record feature as an uncaught exception will fail the entire file.
To Reproduce
Playground: https://repl.it/@RobertPitt/CSV-Paese
const parse = require('csv-parse')
const assert = require('assert')
const data = `a,b,c,d
a,b,c,-` // bad data row
const on_record = ([a, b, c, d]) => {
if(d !== "d") throw new TypeError('Something went wrong')
return [a, b, c, d]
}
const parser = parse(data, {
on_record,
delimiter: ',',
skip_lines_with_error: true,
})
console.log(parser.read())
console.log(parser.read())
Result
Exception is thrown within the on_record call, error breaks stream and throws a global exception which results in a crash.
Expected Result
Exception to be caught and emitted as a skip
event with a CSVError
.
Additional context
My plan is to use the on_record handler to transform the parsed CSV data before it gets emitted out as a data event, however my concern is that if an unexpected error occurs within the on_record call this will result in a crash and loss of the entire file/stream.