【发布时间】:2015-08-13 09:40:02
【问题描述】:
有什么方法可以调试 node.js 的 swig 模板引擎,因为在某些情况下我没有得到发生错误的确切文件, 例如
{% set temp = JSON.parse('some random string') %}
这是我在上面的控制台中得到的:
2015-08-13T09:38:12.254Z - error: SyntaxError: Unexpected token s
at Object.parse (native)
at Object.eval [as tpl] (eval at <anonymous> (/home/saurabh/intl/via_node/node_modules/swig/lib/swig.js:498:13), <anonymous>:6:810)
at compiled (/home/saurabh/intl/via_node/node_modules/swig/lib/swig.js:619:18)
at Object.eval [as tpl] (eval at <anonymous> (/home/saurabh/intl/via_node/node_modules/swig/lib/swig.js:498:13), <anonymous>:27:611)
at compiled (/home/saurabh/intl/via_node/node_modules/swig/lib/swig.js:619:18)
at /home/saurabh/intl/via_node/node_modules/swig/lib/swig.js:559:20
at /home/saurabh/intl/via_node/node_modules/swig/lib/swig.js:690:9
at fs.js:268:14
at Object.oncomplete (fs.js:107:15)
没有提及发生错误的模板文件。
【问题讨论】:
-
严格来说,这不是 Swig/模板问题,而是向
JSON.parse()提供不正确数据的问题。我不认为 Swig 可以捕捉到它们以使其显示它发生的确切位置(无需将每个生成的行包装在try/catch块中)。 -
我在 swig 模板文件中找不到任何 try catch 的规定。
-
没有。我认为在模板中解析 JSON 有点反模式,因此 Swig 中缺乏任何功能来缓解解析过程中发生的任何问题。您可以创建自定义过滤器或标记来捕获错误,但这听起来比将 JSON 解析移动到您的 JS 代码更像是一种折磨。
标签: javascript node.js swig-template