Alexander
Alexander

Reputation: 20244

Javascript eval error

try {
    eval(somejavascript);
} catch(e) {
    console.log(e);
}

gives me runtime errors like

TypeError: Cannot call method 'leftPad' of undefined

Is there any possibility to debug this error, i.e. get the character position/line number where the error occurs in the evaluated expression, and some stack?

The stack I get from chrome ends at eval.

As it goes, I can't put my sample javascript code into a file for debug - the runtime error does not occur if I put the code into a file and include that file in an html document.

Upvotes: 2

Views: 2389

Answers (1)

Tobias
Tobias

Reputation: 7771

I think your question is more about debugging the problem than about the solution.

If so, just wrap the code in a try-catch-statement (for debugging only!):

var jscode = 'function baz() { var foo; foo.leftPad(); } baz();';

eval('try { ' + jscode + ' } catch(err) { console.log(err.stack); }');

You can test it here.

Upvotes: 2

Related Questions