Open basaran opened 3 years ago
+1 I was looking at this too. In my case I use for await
const stream = fs
.createReadStream()
.pipe(csv({ strict: true }))
for await (const line of stream) {
// Do something with line
}
how to handle line error in this case?
This should be done through the stream transformer, but as a quick fix, you can change the error
emit from the module source to something like:
if (!skip && this.options.strict && cells.length !== this.headers.length) {
const e = new RangeError("Row length does not match headers");
e.meta = {
cells: cells,
lineNumber: this.state.lineNumber,
}; /* → injected _meta information */
this.emit("line-error", e);
/* → changed error to line-error so things end, close nicely */
} else {
if (!skip) this.writeRow(cells);
}
and then you can add it to the stream's event handlers with on("line-error", (err) => { console.log('line-error'); })
.
Modifying the node_modules is not a good idea, so if you must, I suggest you clone the repository into your project and do changes there, and require
from there.
Expected Behavior
Not sure
Actual Behavior
It appears, a
range error
event is emitted when strict mode is enabled, which can be listened with theon("error")
as usual.However, when this
range-error
event is emitted, nodejs never emits theon("end")
andon("close")
. I'm assuming this has something to do with thetransform
being broken. This doesn't affect the process most of the time, file executes and exits fine but if you need to do cleanup, it could be problematic as it was in my case, where I needed to disconnect the db once the readstream has finished.Changing the emitted event from
error
toanything
worked and also fired theend
andclose
. Please the sample file and script below to produce the issue. Although I'm not positive if the way it is now is the expected way.Perhaps something is needed in the
transformer
to handle this, if not and it's okay, I recommend changing theerror
toline-error
since this is not really a stream related error.How Do We Reproduce?