Stop node-csv-parser when an error ocurrs

I'm using node-csv-parser (http://www.adaltas.com/projects/node-csv/from.html) in order to parse a CSV file uploaded to a node+express instance. When an error ocurrs into the 'record' event I want to stop the file parsing and return the error to the client, but the 'error' event is called several times (for each record in the CSV file).

Here's my code:

  csv()
  .from(req.files.file.path)
  .on('record', function(data, index){
    var date = data[0];
    var vehicle = data[1];
    var taxId = data[4];
    var customer = data[7];
    var address = data[8] + ' ' + data[9];
    var quantity = data[10]
    console.log(date + ' | ' + vechile + ' | ' + taxId + ' | ' + customer + ' | ' + address + ' | ' + quantity);
  })
  .on('end', function() {
    fs.unlinkSync(req.files.file.path);
    res.end();
  })
  .on('error', function(error) {
    fs.unlinkSync(req.files.file.path);
    console.log('HI');
    res.end('error|' + req.files.file.name);
  });

As you can see above, there's an intentional error when printing to console the full record. The variable called vehicle is wrongly typed (vechile). The 'error' event is then fired multiple times printing 'HI' several times to the console.

What I want is to interrupt the file parsing when the first error occurs, so I can get rid of the file properly (now it's being deleted several times, failing on the second time) and return the error to the client.

I tried using the end() method, but it didn't worked.

Am I on the right track? Any advise?

Thanks in advance

I think I would try to do this. Clean up the file only on the end method. Capture any errors in a variable outside the process, inside the function that uses the csv parser.

Return to the user after you finish reading the csv with the results.

function parseCsv(req, res) {
 var errs = [];
csv()
  .from(req.files.file.path)
  .on('record', function(data, index){
   //
  })
  .on('end', function() {
    fs.unlinkSync(req.files.file.path);
    rs.end(errs);
  })
  .on('error', function(error) {
    errs.push(error);
  });
}

The client can check the return and decide what the response to the client should be. You can move this function into it's own module and take the filePath and a cb as the parameters to further decouple your code, but just besides what you are looking to do. One advantage of doing it like this is that you can present all the errors at once, so the user can fix the file once and re-submit.

The disadvantage is that your are processing the whole file but I don't see a way to stop processing the file inside this library.

I wrote a detailed answer to this issue on GitHub: https://github.com/wdavidw/node-csv-parser/issues/58