0

I'm working on a javascript debugging tool, what I need at the moment is to get the line number off the end of a stack trace. So I wrote the following function to get a stack trace, remove the first few lines and then I was going to use indexOf(':') to get the line number. However I keep getting a "Cannot call method 'substring' of undefined" error. OK that should be an easy one to fix, but wait a minute - console.log suggest the file is defined. Can someone explain where I'm going wrong.

code:

var getLine = function () {
    var stack = (new Error('dummy').stack).toString();
    var stackLines = stack.split('\n');
    stackLines = stackLines.filter(function (element) {
        var exclude = [ "Error: dummy", "graph.js" ];
        for (var i = 0; i < exclude.length; i++) {
            if (element.indexOf(exclude[i]) !== -1) {
                return false;
            }
        }
        return true;
    });

    console.log("1 array", stackLines);
    console.log("2 element", stackLines[0]);
    console.log("3 typeof element", typeof (stackLines[0]));
    console.log("4 huh?", stackLines[0].substring(1));
}

output:

1 array [ '    at Object.<anonymous> (E:\\Development\\james\\main.js:52:18)',
  '    at Module._compile (module.js:456:26)' ]
2 element     at Object.<anonymous> (E:\Development\james\main.js:52:18)
3 typeof element string
E:\Development\james\graph.js:47
    console.log("huh?", stackLines[0].substring(1));
                                      ^
TypeError: Cannot call method 'substring' of undefined

the even stanger thing is - if i wrap the console.log statements in a try/catch then it executes without error?

code:

var getLine = function () {
    var stack = (new Error('dummy').stack).toString();
    var stackLines = stack.split('\n');
    stackLines = stackLines.filter(function (element) {
        var exclude = [ "Error: dummy", "graph.js" ];
        for (var i = 0; i < exclude.length; i++) {
            if (element.indexOf(exclude[i]) !== -1) {
                return false;
            }
        }
        return true;
    });
    try {
        console.log("array", stackLines);
        console.log("element", stackLines[0]);
        console.log("typeof element", typeof (stackLines[0]));
        console.log("huh?", stackLines[0].substring(stackLines[0].indexOf(":")));
    } catch (e){
        console.log("error",e);
    }

output:

1 array [ '    at Object.<anonymous> (E:\\Development\\james\\main.js:52:18)',
  '    at Module._compile (module.js:456:26)' ]
2 element     at Object.<anonymous> (E:\Development\james\main.js:52:18)
3 typeof element string
4 huh? :\Development\james\main.js:52:18)

I feel like I'm missing something very very obvious, but I'm not seeing it!

9
  • This works just fine: jsfiddle.net/55ym1xmj/2
    – vrijdenker
    Sep 28, 2014 at 11:18
  • So is this your actual code, or did you tweak some stuff here to make the example smaller? Because otherwise in your "real" code there may be some kind of race condition.
    – vrijdenker
    Sep 28, 2014 at 11:20
  • Which browser are you using. Stack is a non-standard property of Error. see developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
    – Haim Raman
    Sep 28, 2014 at 11:44
  • @Haim — The question is tagged node.js so, presumably, no browser at all.
    – Quentin
    Sep 28, 2014 at 12:34
  • @JamesG — It would help if you provided sufficient code to reproduce the problem (i.e. test data and a call to your function).
    – Quentin
    Sep 28, 2014 at 12:37

1 Answer 1

0

OK I got to the bottom of it, as I suspected there was a simple explanation:

The function was being called multiple times, the first time stackLines[0] was defined however one of the later times it was undefined. The confusing part is the error was being thrown before the console.logs were being printed.

So the first time it was called all the log statements would work, but none of them were actually output before it through an error in one of the subsequent calls.

When I ran it with the try catch I missed the error as there was a bunch of output and I was scrolling to the top and only checking the first one.

Thanks for the responses everyone :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.