codereviewanskquestions
codereviewanskquestions

Reputation: 13998

Node server crashes in parsing JSON

Looks like my node server dies in parseJSON. Looked at the logs and the last message was "before parse" and it never printed "after parse". What's strange is that I wrapped JSON.pars with try-catch so I am not sure how it caused the server crashed. Any thoughts?

logger.print("before parse")
parseJSON(data)
logger.print("after parse")

and I have pareJSON catch exception.

function parseJSON(str) {
    try {
        var result = JSON.parse(str);
        return result;
    } catch (err) {
        return null
    }
}

Upvotes: 2

Views: 1212

Answers (1)

rsp
rsp

Reputation: 111336

If your code crashes in parseJSON then I would try:

try {
  logger.print("before parse")
  parseJSON(data)
  logger.print("after parse")
} catch (e) {
  console.log(e);
}

It is strange because your function should catch the exception but this would show what happens. I would also add:

console.log(data.length);

to see the size of the data.

Also I wrote a module tryjson that parses JSON without throwing exceptions. You can try using it but if your function crashes then maybe my module would not handle it either. Though I'd love to know what actually happens.

Upvotes: 2

Related Questions