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.?