From 6b13c79fb6f3b8c42ba4f6b11046bf64d55cedb9 Mon Sep 17 00:00:00 2001 From: pbca26 Date: Mon, 10 Apr 2017 12:16:14 +0300 Subject: [PATCH] added json parse error workaround --- routes/cache.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/routes/cache.js b/routes/cache.js index 0630980..03b63ef 100644 --- a/routes/cache.js +++ b/routes/cache.js @@ -22,20 +22,35 @@ 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', - 'result': parsedJSON - }; + var successObj = { + 'msg': 'success', + 'result': parsedJSON + }; - res.end(JSON.stringify(successObj)); + 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 {