节点服务器在解析JSON时崩溃 [英] Node server crashes in parsing JSON
问题描述
好像我的节点服务器死在parseJSON
中.
查看日志,最后一条消息是解析前",而从不打印解析后".奇怪的是我用try-catch包装了JSON.pars,所以我不确定它是如何导致服务器崩溃的.有什么想法吗?
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")
并且我有pareJSON catch异常.
and I have pareJSON catch exception.
function parseJSON(str) {
try {
var result = JSON.parse(str);
return result;
} catch (err) {
return null
}
}
推荐答案
如果您的代码在parseJSON中崩溃,那么我会尝试:
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);
查看数据大小.
我还编写了一个模块 tryjson
,该模块可以解析JSON而不会引发异常.您可以尝试使用它,但是如果您的函数崩溃了,也许我的模块也不会处理它.虽然我很想知道实际发生了什么.
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.
这篇关于节点服务器在解析JSON时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!