error.stack 中的 coffeescript 行号?

标签 coffeescript

以下 CoffeeScript 代码:

try
    any_error_here
catch err
    alert err.stack

给出以下堆栈跟踪:

ReferenceError: any_error_here is not defined 
  at eval (coffeescript:5:3)

如何获取此堆栈(第 2 行)的 coffeescript 行号?

这是一个简单的 html,有同样的问题:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Coffee-script stack trace problems</title>
        <script type="text/javascript" language="javascript" src="http://coffeescript.org/extras/coffee-script.js"></script>
    </head>
    <body id="dt_example">
        <script type="text/coffeescript" language="javascript">
    try
        any_error_here
    catch err
        alert err.message+'\n'+err.stack + '\n\nWhat is coffeescript line number for this stack?'
        </script>
    </body>
</html>

最佳答案

这听起来像是 source maps 的问题.

当你编译你的 CoffeeScript 时,你可以通过指定 -m 选项来生成源映射:

coffee -c -m so.coffee

在支持源映射的浏览器中(例如 Chrome),CoffeeScript 文件将出现在 Sources 选项卡中,错误和日志语句将引用 CoffeeScript 文件中的相应行。

Chrome developer console with source-mapped CoffeeScript

有关更多信息,请查看 HTML5 tutorial on source maps , 和一个 short article about using source maps with CoffeeScript .

如果您有源映射,您还可以使用它来解析堆栈跟踪字符串并引用原始未编译文件,如 this question 中所述。 .

关于error.stack 中的 coffeescript 行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21461971/

相关文章:

javascript - 处理许多 DOM 更改

javascript - 输入字段占位符属性 ie7+

node.js - 如何使用 mocha 测试基本的 javascript 文件?

javascript - 在浏览器和 Node 中运行 CoffeeScript 类

node.js - 使用node.js和lazy时了解EOF

javascript - coffeescript - 我们可以在 ruby​​ on rails 的 View 文件夹中使用 coffeescript 代码吗?

http - ngMock 无法识别请求

coffeescript - Wintersmith:错误加载插件 './node_modules/wintersmith-coffee/' 时出错:找不到模块 './plugin'

javascript - 在 Coffee.vim 中修改注释突出显示时出现问题

javascript - 无需安装编译器即可编译嵌入式 CoffeeScript?