[removed] debug stack trace with source maps

后端 未结 3 992
我寻月下人不归
我寻月下人不归 2021-01-11 11:23

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 m

相关标签:
3条回答
  • 2021-01-11 11:43

    Found this: https://github.com/thlorenz/stack-mapper

    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.

    Edit Actually, this one works a lot better and is much simpler to use https://github.com/mozilla/source-map/.

    Example Usage:

    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]));
    
    0 讨论(0)
  • 2021-01-11 11:43

    I found https://github.com/thlorenz/stack-mapper easy to use to do this automatically. If you are looking to do this interactively, you can use stack-mapper in a web browser with a tool like this one: https://github.com/Jimbly/stackwalker - just load your .map file, paste the (minified) callstack or single error location and then you can click through the stack and see the original code on the right.

    0 讨论(0)
  • 2021-01-11 12:00

    stacktrace.js looks to be another useful tool to achieve this.

    Example from their website:

    var error = new Error('BOOM!');
    StackTrace.fromError(error).then(callback).catch(errback)
    => Promise(Array[StackFrame], Error);
    
    0 讨论(0)
提交回复
热议问题