Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Stop parsing #58

Closed
jechenique opened this Issue · 2 comments

2 participants

jechenique Worms David
jechenique

Hi,
is there any way to stop the file process in case that an error ocurrs?
I've been trying to do it with the end method but it doesn't works.
I've posted a question here about it: http://stackoverflow.com/questions/13200136/stop-node-csv-parser-when-an-error-ocurrs

Thanks

Worms David
Owner

Thanks for your report, I'll look at it in the next few days

Worms David
Owner

In its latest version (0.2.2), the parser use a pipe implementation when you call csv().from.stream(...). However, and you can check it by yourself in the Node.js source code, the pipe function doesn't call the source destroy function when an error occured in the destination. It is your responsibility. I check on my own with a large source file, and it work with this example:

source = __dirname+'/test.csv';
source = fs.createReadStream(source);
source.on('end', function(){ console.log('source end') })
source.on('error', function(){ console.log('source error') })
source.on('close', function(){ console.log('source close') })

csv()
  .from(source)
  .on('record', function(data, index){
    var date = data[0];
    var vehicle = data[1];
    error = date + ' | ' + vechile;
  })
  .on('end', function() {
    console.log('csv end');
  })
  .on('error', function(error) {
    source.destroy();
    console.log('csv error');
  });

Yes, error is called multiple times because destroy is an async operation but the source stream is closed before it reach the end of the file.

At this point, it is your responsibility to implement the logic where the code handling the error is only called once. However, we could add an option to the parser like "stopOnError" to prevent the error listener to be called more than once. In such case, if you find it interesting, then please open a new issue (and please suggest me a better name: ).

David

Worms David wdavidw closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.