第三方包_json parse better errors - dkvirus/npm-resource-read GitHub Wiki
使用 JSON.parse() 如果出错会打印错误信息,json-parse-better-errors 号称打印更有用的错误信息,just this!!
使用 JSON.parse() 出错打印的报错信息:
JSON.parse('garbage')
/**
undefined:1
garbage
^
SyntaxError: Unexpected token g in JSON at position 0
at JSON.parse (<anonymous>)
at Object.<anonymous> (/Users/dkvirus/Documents/npm-test/test/json-parse-better-errors.js:4:6)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
*/
使用 json-parse-better-errors 打印的错误信息:(使用前先安装:npm install json-parse-better-errors)
const parseJson = require('json-parse-better-errors')
parseJson('garbage') // more useful error message
/**
/Users/dkvirus/Documents/npm-test/test/node_modules/json-parse-better-errors/index.js:36
throw e
^
SyntaxError: Unexpected token g in JSON at position 0 while parsing near 'garbage'
at JSON.parse (<anonymous>)
at parseJson (/Users/dkvirus/Documents/npm-test/test/node_modules/json-parse-better-errors/index.js:7:17)
at Object.<anonymous> (/Users/dkvirus/Documents/npm-test/test/json-parse-better-errors.js:24:1)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
*/
恕在下眼拙,并没有看出什么有价值的错误信息。