I have been battling this issue for half a week to no avail, my problem is:
using Node.js 0.10.12 to parse JSON files like so:
var invalidJson = '{ this is bad }';
try {
JSON.parse( invalidJson );
}
catch (exc) {
console.log(exc.stack);
throw exc;
}
the output:
SyntaxError: Unexpected token t
at Object.parse (native)
at Object.<anonymous> (test.js:7:10)
...
at node.js:901:3
test.js:12
throw exc;
..And then a duplicate of 'SyntaxError: Unexpected token t..' because I re-throw the exception
Now, when doing:
JSON.parse( invalidJson );
without try {} catch {}
I get this error:
undefined:1
{ this is bad }
^
SyntaxError: Unexpected token t
at Object.parse (native)
at Object.<anonymous> (test.js:17:6)
...
at node.js:901:3
this error message tells me exactly where the JSON parsing broke. When the JSON file is large, it's virtually impossible to locate the error without these details.
How can I forward this descriptive error message in my custom try{}catch{} exception handler?
thanks!