简体   繁体   English

如何获取缩小的 javascript 堆栈跟踪并针对源映射运行它以获得正确的错误?

[英]How can I take a minified javascript stack trace and run it against a source map to get the proper error?

On our production server, I have minified javascript published and I'm not including a map file with it, because I don't want the user to be able to understand what's happening based on the error.在我们的生产服务器上,我已经缩小了已发布的 javascript 并且我没有在其中包含地图文件,因为我不希望用户能够根据错误了解发生了什么。

I have a logging service I've written to forward the angular exceptions (caught by $exceptionHandler) to myself via email.我编写了一个日志服务,用于通过电子邮件将角度异常(由 $exceptionHandler 捕获)转发给自己。 However, this stack trace is near unreadable:但是,此堆栈跟踪几乎无法读取:

n is not defined
    at o (http://localhost:9000/build/app.min.js:1:3284)
    at new NameController (http://localhost:9000/build/app.min.js:1:3412)
    at e (http://localhost:9000/build/bower.min.js:44:193)
    at Object.g.instantiate (http://localhost:9000/build/bower.min.js:44:310)
    at b.$get (http://localhost:9000/build/bower.min.js:85:313)
    at d.compile (http://localhost:9000/build/bower.min.js:321:23333)
    at aa (http://localhost:9000/build/bower.min.js:78:90)
    at K (http://localhost:9000/build/bower.min.js:67:39)
    at g (http://localhost:9000/build/bower.min.js:59:410)
    at http://localhost:9000/build/bower.min.js:58:480 <ui-view class="ng-scope">

What I'm wondering is: Is there a program where I can analyze this stack trace against the actual non-minified source code via map file (or not via map file if there's another way)我想知道的是:是否有一个程序可以通过映射文件(或者如果有另一种方法,则不通过映射文件)针对实际的非缩小源代码分析此堆栈跟踪

What you want to do is parse the source maps.您要做的是解析源映射。 This has nothing to do with web browsers.这与网络浏览器无关。 All you need to do is translate the minified reference into the unminified resource.您需要做的就是将缩小的引用转换为未缩小的资源。

If you have any experience with NodeJS there is already a package that does this for you.如果您对 NodeJS 有任何经验,那么已经有一个包可以为您执行此操作。

https://github.com/mozilla/source-map/ https://github.com/mozilla/source-map/

To install the library安装库

npm install -g source-map

or或者

yarn global add source-map

Create a file named "issue.js"创建一个名为“issue.js”的文件

fs = require('fs');
var sourceMap = require('source-map');
var smc = new sourceMap.SourceMapConsumer(fs.readFileSync("./app.min.js.map","utf8"));
console.log(smc.originalPositionFor({line: 1, column: 3284}));

Run the file with node使用节点运行文件

node issue.js

It should output the location in the original file to the console for first line from the stack trace.它应该将原始文件中的位置输出到堆栈跟踪中第一行的控制台。

Note: I tell you install source-map globally for ease of use, but you could create a node project that does what you need and installs it locally.注意:为了便于使用,我告诉您全局安装 source-map,但是您可以创建一个节点项目来执行您需要的操作并在本地安装它。

I figured there was no super simple tool for converting a minified stack trace into a readable one using a source map (without having to use a web service), so I created a tool for it:我认为没有超级简单的工具可以使用源映射(无需使用 Web 服务)将缩小的堆栈跟踪转换为可读的堆栈跟踪,因此我为此创建了一个工具:

https://github.com/mifi/stacktracify https://github.com/mifi/stacktracify

Install and use it as follows:安装和使用如下:

npm install -g stacktracify

Now copy a minified stacktrace to your clipboard - then run:现在将缩小的堆栈跟踪复制到剪贴板- 然后运行:

stacktracify /path/to/js.map

Adding to @Reactgular's answer, the below snippet will work with the latest version of source-map添加到@Reactgular 的答案中,以下代码段将适用于最新版本的 source-map

  const rawSourceMap = fs.readFileSync("./app.min.js.map","utf8");

  const whatever = sourceMap.SourceMapConsumer.with(rawSourceMap, null, consumer => {
    console.log(consumer.originalPositionFor({
      line: 1,
      column: 3284
    }));
  });

And to add to the discussion on the thread a simple regex like /\\/(\\w*[-\\.]?\\w*).js:\\d*:\\d*/g并在线程上添加一个简单的正则表达式,如/\\/(\\w*[-\\.]?\\w*).js:\\d*:\\d*/g

Below is a very simple regex to find all line numbers in a stacktrace.下面是一个非常简单的正则表达式,用于在堆栈跟踪中查找所有行号。

  //regex for patterns like utils.js, utils123.js, utils-name.js, utils.version.js
  var patt = /\/(\w*[-\.]?\w*).js:\d*:\d*/g; 
  // returns matches like ['/app.min.js:1:3284', '/bower.min.js:44:193', ..]
  var errorPositions = line.match(patt);
  console.log(errorPositions);
  if(!errorPositions || errorPositions.length === 0) {
      console.log("No error line numbers detected in the file. Ensure your stack trace file is proper");
      return;
  }
  errorPositions.forEach(function(error) {
    findInSourceMap(error);
  });
});

If you had access to the source map file externally and could get the same file structure you could work it out I guess, but I'm not aware of any tools outside the browser that will help you with that.如果您可以从外部访问源映射文件并且可以获得相同的文件结构,我想您可以解决它,但我不知道浏览器之外的任何工具可以帮助您解决这个问题。

The added advantage of having the data in a running browser will allow checking of locals which you won't get even with a source map.在正在运行的浏览器中保存数据的额外优势将允许检查本地人,即使使用源地图也无法获得。

You might want to consider a tool such as rollbar to do error reporting.您可能需要考虑使用rollbar 之类的工具来报告错误。 This will report all the locals in each frame to help debugging.这将报告每个帧中的所有局部变量以帮助调试。 It has support for sourcemaps outside the browser to address your security concerns.它支持浏览器外的源地图,以解决您的安全问题。

Append comment directive for the JS running in the page.为页面中运行的 JS 附加注释指令。

//# sourceMappingURL=/path/to/your/sourcemap.map //# sourceMappingURL=/path/to/your/sourcemap.map

In firefox (not sure about chrome) to tell the Debugger to use source maps if they are available, click the "Debugger settings" button and select "Show original sources" from the list of settings that pops up:在firefox(不确定chrome)中告诉调试器使用源映射(如果它们可用),单击“调试器设置”按钮并从弹出的设置列表中选择“显示原始源”:

暂无
暂无

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

相关问题 如何获取 webpack 生产环境的可读堆栈跟踪,用于将开发工具设置为源映射的缩小捆绑包? - How to get a readable stack trace for a webpack production environment for a minified bundle with dev-tool set to source-map? 如何从缩小的javascript文件中获取源代码? - How can i get source from a minified javascript file? 如何在浏览器中使用内联源代码 map 评估代码以获得适当的异常堆栈? - How can I eval code with inline source map in browser to get a proper stack in exception? 如何在 IE 8 中获取 JavaScript 异常的堆栈跟踪? - How can I get the stack trace for a JavaScript exception in IE 8? 如何在Firefox中获取JavaScript堆栈跟踪? - How can I get a JavaScript stack trace in Firefox? 抛出异常时如何获取 JavaScript 堆栈跟踪? - How can I get a JavaScript stack trace when I throw an exception? 我可以从缩小版本和相应的源映射文件重新构建原始 JavaScript 源文件吗? - Can I re-construct the original JavaScript source file from a minified version and the corresponding source-map file? 如何为在生产中使用缩小的javascript代码的项目修改和测试javascript源代码? - How can I modify and test javascript source code for a project which uses minified javascript code in production? 如何在不停止脚本的情况下获取JS堆栈跟踪? - How can I get a JS stack trace without halting the script? 如何使用承诺查看 setTimeout 中错误的完整堆栈跟踪 - How can I see the full stack trace of error in setTimeout with a promise
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM