Javascript:使用源地图调试堆栈跟踪 [英] Javascript: debug stack trace with source maps

查看:158
本文介绍了Javascript:使用源地图调试堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个奇怪的问题,我似乎无法搜索正确的短语来提取任何相关的答案。

This may be a bit of an odd question, I can't seem to search the right phrase to pull up any relevant answers.

我们有一个应用程序运行在客户端机器上,并被细化。我们生成源地图,但没有暴露于生产版本。

We have an app that runs on clients machines and is minified. We generate source maps, but they are not exposed to production versions.

我有一个 window.onerror 使用捕获所有的马虎代码,发现它的方式在我不知道的。幸运的是,这几乎从未被利用。最近,我一直在收到一个未定义的错误错误,所以有人找到了一种方式来做某些不希望的事情。 Chrome在堆栈跟踪中记录行号和列号,当logerror捕获其中一个bug时,我们登录到日志记录服务器,但是我必须进行调试,并通过一个min文件查看不太有吸引力。而 undefined不是一个函数也不是很有帮助:)

I have a window.onerror that I use a catch all for sloppy code that finds it's way in unbeknownst to me. Thankfully, this is almost never utilized. Recently, I've been getting an undefined error popping up occasionally so someone has found a way to do something not intended. Chrome does a nice job recording row and column number in the stack trace which we log to a logging server when onerror catches one of these buggers, but that's all I have to debug with and looking through a min file is less than appealing. And undefined is not a function is not very helpful either :)

问题:有一个工具在那里 - 可能在nodejs中,可以使用最小文件,源映射和堆栈跟踪字符串,并生成相关文件,行号和列号?

Question: is there a tool out there - maybe in nodejs - that can take a min file, source map, and a stack trace string and produce relevant files, line numbers, and column numbers?

我意识到浏览器在运行时为您执行此操作,但在这种情况下,我没有这样的奢侈品,因为我正在试图找出错误实际事实之后。

I realize that the browser does this for you at runtime, but in this case I don't have that luxury as I'm trying to figure out what the error actually is after the fact.

推荐答案

找到这个: https://github.com/thlorenz/stack-mapper

我使用的uglify似乎产生了正确的映射这个需要,它看起来像上面提到的情况一样。

I use uglify which seems to produce the correct mapping that this needs and it looks like it will work for the case I suggested above.

修改
其实这个好多了,使用起来要简单得多 https://github.com/mozilla/source-map/

示例用法:

var fs = require('fs');
var smc = require('source-map');

var stack = "TypeError: undefined is not a function\r\nat h/min/min.js?1404839824760:9:23048";
stack = stack.split(/\r\n/g);
var error = stack.shift(); // First line is the actual error

var errors = [];
var file = null;

stack.forEach(function(line){
    var _trace = line.split('/').pop();
    var trace = {};
    trace.filename = _trace.split('?').shift();
    _trace = _trace.split(':');
    trace.line = parseInt(_trace[1], 10);
    trace.column = parseInt(_trace[2], 10);
    errors.push(trace);

    if(!file)
        file = trace.filename.split('.').shift();

    trace.filename = __dirname + '/../min/' + trace.filename;
});

// This does not account for multiple files in stack trace right now
var map = fs.readFileSync(__dirname + '/../src/' + file + '.js.map');
map = JSON.parse(map);
var sm = new smc.SourceMapConsumer(map);
console.log(sm.originalPositionFor(errors[0]));

这篇关于Javascript:使用源地图调试堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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