mafintosh / mafintosh/csv-parser

strict mode emit error

Open
#194 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
1.5k
Forks
143
PR merge metrics
No merged PRs in 30d

Description

  • Operating System: WSL
  • Node Version: 15.8
  • NPM Version: 7.5.1
  • csv-parser Version: 3.0.0
Expected Behavior

Not sure

Actual Behavior

It appears, a range error event is emitted when strict mode is enabled, which can be listened with the on("error") as usual.

However, when this range-error event is emitted, nodejs never emits the on("end") and on("close"). I'm assuming this has something to do with the transform 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 to anything worked and also fired the end and close. 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 the error to line-error since this is not really a stream related error.

       if (!skip && this.options.strict && cells.length !== this.headers.length) {
            const e = new RangeError("Row length does not match headers");
            this.emit("error", e);
How Do We Reproduce?
head1,head2,head3
a,b,c
x,x,x
e,e
y,y,y
f,f
const csvParser = require("csv-parser");
const fs = require("fs");

const rs = fs.createReadStream("./a.csv");

rs.pipe(
    csvParser({
        strict: true,
    })
)
.on("error", () => {
    console.log('caught');
})
.on("end", function () {
    console.log("All the data in the file has been read");
})
.on("close", function (err) {
    console.log("Stream has been destroyed and file has been closed");
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the strict-mode branch that emits the RangeError and follow the csvParser stream through the error, end, and close handlers in the reproduction. Run the provided CSV and Node script, then verify the intended terminal-event behavior for malformed rows.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
stream-processing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.