Browse Source

added json parse error workaround

all-modes
pbca26 8 years ago
parent
commit
6b13c79fb6
  1. 25
      routes/cache.js

25
routes/cache.js

@ -22,13 +22,10 @@ cache.get = function(req, res, next) {
res.end(JSON.stringify(errorObj));
} else {
var parsedJSON = 'JSON parse error';
//var parsedJSON = 'JPARSE';//'JSON parse error';
try {
parsedJSON = JSON.parse(data);
} catch (e) {
console.log('JSON parse error');
}
var parsedJSON = JSON.parse(data);
var successObj = {
'msg': 'success',
@ -36,6 +33,24 @@ cache.get = function(req, res, next) {
};
res.end(JSON.stringify(successObj));
} catch (e) {
console.log(e);
if (e.toString().indexOf('at position') > -1) {
const errorPos = e.toString().split(' ');
//console.log(errorPos[errorPos.length - 1]);
//JSON.parse(data.substring(0, errorPos[errorPos.length - 1]));
console.log('JSON error ---> ' + data.substring(errorPos[errorPos.length - 1] - 20, errorPos[errorPos.length - 1] + 20) + ' | error sequence: ' + data.substring(errorPos[errorPos.length - 1], errorPos[errorPos.length - 1] + 1));
console.log('attempting to recover JSON data');
fs.writeFile(cache.iguanaDir + '/shepherd/cache-' + pubkey + '.json', data.substring(0, errorPos[errorPos.length - 1]), function(err) {
var successObj = {
'msg': 'success',
'result': data.substring(0, errorPos[errorPos.length - 1])
};
res.end(JSON.stringify(successObj));
});
}
}
}
});
} else {

Loading…
Cancel
Save