缺lessNodeJS JSON.parse中的exception信息
在NodeJS中,如果我让JSON.parseexception不能处理,并随后导致服务器崩溃,那么我会得到一些输出到控制台的信息。
我的JSON:
{ "familyName": "The Flintstones", "familyContact": { "fullName": "Wifie Flintstone", "contact": { "street1": "1 Granite Ave", "city": "Bedrock", "state": "TX", "postal": "10000" } }, this should make it blow up "patients": [{ "personInfo": { "fullName": "Wilma Flintstone" }}, { "personInfo": { "fullName": "Fred Flintstone" } } ] }
这是控制台输出,当我让它去处理:
undefined:12 this should make it blow up ^ SyntaxError: Unexpected token t at Object.parse (native) at Object.exports.uploadPackage.flowUploads.post.flowUploads.write.onDone (/home/ubuntu/workspace/app/controllers/admin/admin.families.server.controller.js:99:27) at module.exports.$.write.pipeChunk (/home/ubuntu/workspace/app/services/flowUploads.js:173:49) at Object.cb [as oncomplete] (fs.js:169:19)
但如果我赶上例外,以防止崩溃,并像这样的日志…
try{ var fileData = JSON.parse(fileText); } catch (ex) { console.log("REASON FOR JSON FAILURE: " + ex, ex.arguments, ex.stack.split("\n")); }
我得到这样的输出:
REASON FOR JSON FAILURE: SyntaxError: Unexpected token t [ 't' ] [ 'SyntaxError: Unexpected token t', ' at Object.parse (native)', ' at Object.exports.uploadPackage.flowUploads.post.flowUploads.write.onDone (/home/ubuntu/workspace/app/controllers/admin/admin.families.server.controller.js:100:27)', ' at module.exports.$.write.pipeChunk (/home/ubuntu/workspace/app/services/flowUploads.js:173:49)', ' at Object.cb [as oncomplete] (fs.js:169:19)' ]
缺less包含行号和错误文本片段的崩溃日志中的关键信息:
undefined:12 this should make it blow up ^
如何才能达到这一点,所以我可以将其纳入我的错误消息?
你不能从错误中得到你显示的第一位,因为它不是错误的一部分。
undefined:12 this should make it blow up ^
这是因为节点在死亡之前添加该部分来显示为什么它死了,然后显示由JSON.parse生成的错误。
要获得更详细的消息,你将不得不使用JSON.parse。 正如robertklep提到你可以尝试greatjson或类似的东西,如果你的JSONparsing得到更多的细节是很重要的,比如,如果你正在build立一个parsingJSON并返回错误的服务。