gpt4 book ai didi

error.stack 中的 coffeescript 行号?

转载 作者:行者123 更新时间:2023-12-01 14:19:51 25 4
gpt4 key购买 nike

以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com