如何获取缩小的 javascript 堆栈跟踪并针对源映射运行它以获得正确的错误? [英] How can I take a minified javascript stack trace and run it against a source map to get the proper error?

查看:21
本文介绍了如何获取缩小的 javascript 堆栈跟踪并针对源映射运行它以获得正确的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的生产服务器上,我已经缩小了已发布的 javascript 并且我没有在其中包含地图文件,因为我不希望用户能够根据错误了解发生了什么.

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.

我编写了一个日志服务,用于通过电子邮件将角度异常(由 $exceptionHandler 捕获)转发给我自己.但是,此堆栈跟踪几乎无法读取:

I have a logging service I've written to forward the angular exceptions (caught by $exceptionHandler) to myself via email. 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.

如果您对 NodeJS 有任何经验,那么已经有一个包可以为您执行此操作.

If you have any experience with NodeJS there is already a package that does this for you.

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

安装库

npm install -g source-map

yarn global add source-map

创建一个名为issue.js"的文件

Create a file named "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}));

使用节点运行文件

node issue.js

它应该将原始文件中的位置输出到控制台作为堆栈跟踪的第一行.

It should output the location in the original file to the console for first line from the stack trace.

注意:为了便于使用,我告诉您全局安装 source-map,但您可以创建一个节点项目来执行您需要的操作并在本地安装它.

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.

这篇关于如何获取缩小的 javascript 堆栈跟踪并针对源映射运行它以获得正确的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆