简体   繁体   English

利用源映射的堆栈跟踪

[英]Stack traces that utilise source mapping

Overview:概述:
The stack trace output in the browser console is not the same trace that is returned when Error.stack is called.浏览器控制台中的堆栈跟踪输出与调用 Error.stack 时返回的跟踪不同。 The console stack trace seems to take into account sourcemaps whereas the Error.stack stack trace does not.控制台堆栈跟踪似乎考虑了源映射,而 Error.stack 堆栈跟踪则没有。

Console Output控制台输出
Here is the default stack trace that is output to the console.这是输出到控制台的默认堆栈跟踪。

Uncaught TypeError: Cannot set property 'y' of undefined source.js:4
    (anonymous function) source.js:4
    (anonymous function) source.js:4
    (anonymous function) (index):17

Error.stack Output错误堆栈输出
Here is the stack trace from Error.stack:这是来自 Error.stack 的堆栈跟踪:

TypeError: Cannot set property 'y' of undefined
    at <anonymous>:1:37
    at <anonymous>:1:60
    at http://localhost:63342/source-map-example/example2/:17:23 (index):12

Source Code:源代码:
Here is the code that I used for this experiment:这是我用于此实验的代码:

<script>
    window.onerror = function() {
        console.log(arguments[4].stack);
    }

    var script = document.createElement('script');
    script.textContent = '(function(){var person={};person.x.y="Throws an error..."})();//# sourceMappingURL=source.min.map';
    document.body.appendChild(script);
</script>

Question:题:
Is it possible to programmatically obtain a stack trace that includes references to files & lines based on the associated sourcemap?是否可以以编程方式获取包含基于关联源映射的文件和行引用的堆栈跟踪?

Edit: console.trace and new Error().stack编辑:console.trace 和 new Error().stack

note: I didn't use window.onerror for these examples, instead I wrapped the embedded JS in a try...catch and attempted to utilise these approaches within the catch.注意:我没有在这些示例中使用 window.onerror,而是将嵌入的 JS 包装在 try...catch 中,并尝试在 catch 中使用这些方法。 The reason for this was because the stack trace didn't provide any trace into the embedded JS for either method when used within window.onerror.这样做的原因是,当在 window.onerror 中使用时,堆栈跟踪没有为任何一种方法提供任何到嵌入式 JS 的跟踪。

console.trace() works the best, but of course the output can't be captured. console.trace() 效果最好,但当然无法捕获输出。 Even still, this does not work as expected.即使如此,这也不能按预期工作。 The output contains a stack trace that points to the console.trace() line, and little else.输出包含指向 console.trace() 行的堆栈跟踪,以及其他一些内容。

console.trace() source.js:9
    (anonymous function) source.js:9
    (anonymous function) source.js:9
    (anonymous function)

new Error().stack does not work as expected either. new Error().stack 也不能按预期工作。 It does contain a stack trace, but it's not using the sourcemap.它确实包含一个堆栈跟踪,但它没有使用源映射。

Error
    at <anonymous>:1:85
    at <anonymous>:1:105
    at http://localhost:63342/source-map-example/example2/:18:23 source.js:18

Unfortunately, this is browser-specific feature is not currently supported in either Firefox or Chrome (and I am unfamiliar with its support in other browsers).不幸的是,目前 Firefox 或 Chrome 都不支持此特定于浏览器的功能(而且我不熟悉它在其他浏览器中的支持)。

In Chrome, you can follow feature request currently being implemented at https://code.google.com/p/chromium/issues/detail?id=357958在 Chrome 中,您可以关注当前正在实施的功能请求https://code.google.com/p/chromium/issues/detail?id=357958

Depending on your use case and if you have some means of capturing the sourcemaps themselves, Mozilla has an excellent sourcemapping tool that will let you map your sources/stacktraces at https://github.com/mozilla/source-map/ .根据您的用例,如果您有某种方法自己捕获源映射,Mozilla 有一个出色的源映射工具,可以让您在https://github.com/mozilla/source-map/映射源/堆栈跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM