Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop parsing #58

Closed
jechenique opened this issue Nov 3, 2012 · 2 comments
Closed

Stop parsing #58

jechenique opened this issue Nov 3, 2012 · 2 comments

Comments

@jechenique
Copy link

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

@wdavidw
Copy link
Member

wdavidw commented Nov 4, 2012

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

@wdavidw
Copy link
Member

wdavidw commented Nov 5, 2012

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

@wdavidw wdavidw closed this as completed Nov 5, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants