0

I am using csvConvertor to convert csv file to json. when I import corrupt csv file.Server crash and shows
throw ("Incomplete CSV file detected. Quotes does not match in pairs.");
Here is my code

exports.addAsset = function (req, res, next) {
  var fleets = req.body;
  //Todo: Request validation function- Asynchronous

  var result = [],
    fleetResult;
  async.each(fleets,
    function (fleetData, callback) {
      //Todo: Check if levelId exists?
      async.waterfall([
      function (cb) {
          if (fleetData.type == "csv") {
            var csvstring,
              Converter = require("csvtojson").core.Converter,
              param = {},
              csvConverter = new Converter(param);
              //convert base64 to binary
              csvString = new Buffer(fleetData.source, 'base64').toString('binary'),
                //convert binary data of csv file to json object
                //end_parsed will be emitted once file parsing is finished
                csvConverter.on("end_parsed", function (jsonObj) {
                  cb(null, camelize(jsonObj))
                });
              csvConverter.fromString(csvString);         
          } else {
            cb(null, fleetData.source);
          }
      },
      function (jsonObj, cb) {
          //Todo: write method to convert given json object to asset- as per the "Asset" Model.
          setupAsset(jsonObj, cb)
      },
        //insert assets in the jsonObj array to database
      function (jsonObj, cb) {
          insertAsset(jsonObj, fleetData.levelId, cb)
      },
      function (insertStatus, cb) {
          //populate result for fleet import
          fleetResult = {
            "importedAssets": insertStatus.importedAssets,
            "importFailedAssets": insertStatus.importFailedAssets
          }
          result.push(fleetResult);
          callback();
      }
    ], function (err) {
        if (err)
          result.push(err);
      });
    },
    function () {
      next(null, result);
    });
}

If I import uncorrupt csv file, It imports successfully, but if I import currupt csv file It crashes the server. How to handle this exception.?

shubham
  • 1
  • 5
  • I think format you are importing is not comma separated so it is giving this error. You can see for different option that csvtojson might be accepting for delimiter or you can change file which is comma separated. – KlwntSingh May 04 '15 at 11:25
  • I am importing a file which is not a comma separated but have csv extention that is corrupted file. I made this corrupted file manually. That is why my server is going crashed. But I want the solution to handle this type of error so that my server should not crash, If user import curropted file. – shubham May 04 '15 at 11:30
  • check if async.waterfall([ ]) check functions in array of function you are not calling cb – KlwntSingh May 04 '15 at 11:54

0 Answers0