Draco
Draco

Reputation: 16354

Getting line number of an error from minified JavaScript files

Is it in any way possible to dynamically get the line number of an error when the JavaScript files are minified?

At the moment all errors are logged as being occurred on line 0.

Upvotes: 1

Views: 2829

Answers (3)

technosaurus
technosaurus

Reputation: 7812

One reason that you would want to debug a minified script is if you were using the closure compiler to optimize it and the optimization process caused a bug. For browsers that supply the column (Chrome,IE) in the stack trace you can do something like:

/*@const*/ //for closure-compiler
DEBUG=2 // 0=off, 1=msg:file:line:column, 2=msg:stack-trace
if(DEBUG){

/*@const @constructor*/
Object.defineProperty(window,'__stack__',{get:function(){
    try{_ფ_()}catch(e){return e.stack.split(":")}
}})

/*@const @constructor*/
Object.defineProperty(window,'__file__',{get:function(){
    var s=__stack__,l=s.length
    return (isNaN(s[l-2]))?s[l-2]:s[l-3]
}})

/*@const @constructor*/
Object.defineProperty(window,'__line__',{get:function(){
    var s=__stack__,l=s.length
    return (isNaN(s[l-2]))?s[l-1]:s[l-2]
}})

/*@const @constructor*/
Object.defineProperty(window,'__col__',{get:function(){
    var s=__stack__,l=s.length
    return (isNaN(s[l-2]))?"NA":s[l-1]
}})

/*@const @constructor*/
Object.defineProperty(window,'LOG',{
    get:function(){return out},
    set:function(msg){if(DEBUG>1)out=msg+"\t-\t"+__stack__
        else out=msg+" in file:"+__file__+" @ Line:"+__line__+", Column:"+__col__
        console.log(out)}
})
}//end if(DEBUG)

Usage: LOG="my message" to write "my message" along with the line number and file to the console or to get the last log alert(LOG)

You can get more into the weeds with v8 (chrome, node.js)

/*@const @constructor*/ Object.defineProperty(this,'__stack',{get:function(){var o=Error['prepareStackTrace'],e=new Error,s;Error['prepareStackTrace']=function(_,s){return s},Error['captureStackTrace'](e,arguments['callee']),s=e['stack'],Error['prepareStackTrace']=o;return s}})

/*@const @constructor*/ Object.defineProperty(this,'__col__',{get:function(){return __stack[1]['getColumnNumber']()}})
/*@const @constructor*/ Object.defineProperty(this,'__eval_orig__',{get:function(){return __stack[1]['getEvalOrigin']()}})
/*@const @constructor*/ Object.defineProperty(this,'__file__',{get:function(){return __stack[1]['getFileName']()}})
/*@const @constructor*/ Object.defineProperty(this,'__func__',{get:function(){return __stack[1]['getFunctionName']()}})
/*@const @constructor*/ Object.defineProperty(this,'__function__',{get:function(){return __stack[1]['getFunction']()}})
/*@const @constructor*/ Object.defineProperty(this,'__is_constructor__',{get:function(){return __stack[1]['isConstructor']()}})
/*@const @constructor*/ Object.defineProperty(this,'__is_eval__',{get:function(){return __stack[1]['isEval']()}})
/*@const @constructor*/ Object.defineProperty(this,'__is_native__',{get:function(){return __stack[1]['isNative']()}})
/*@const @constructor*/ Object.defineProperty(this,'__is_top_level__',{get:function(){return __stack[1]['isTopLevel']()}})
/*@const @constructor*/ Object.defineProperty(this,'__line__',{get:function(){return __stack[1]['getLineNumber']()}})
/*@const @constructor*/ Object.defineProperty(this,'__method__',{get:function(){return __stack[1]['getMethodName']()}})
/*@const @constructor*/ Object.defineProperty(this,'__this__',{get:function(){return __stack[1]['getThis']()}})
/*@const @constructor*/ Object.defineProperty(this,'__type__',{get:function(){return __stack[1]['getTypeName']()}})

Upvotes: 1

Florian Margaine
Florian Margaine

Reputation: 60787

You can beautify minified code. In the chrome inspector, it is the {} button and is called "pretty print".

However, beautifying this code doesn't mean it will respect your original code.

Thus, I'll say what ThiefMaster said: don't use minified code during development/debugging.

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318558

No. That's why you shouldn't use minified code during development/debugging.

Upvotes: 3

Related Questions