The Parable of the Missing Stack

    - Hello, my friend ...
    - Uncle, will you tell me a story again?
    - Of course! What a smart boy!
    - And about what?
    - M ... today I will tell you about where too independent boys like you disappear ...
    - Uh! What does it mean to disappear?
    - That’s what it means ... On, hold fireside * extended, holding by the tail, an animal glowing with flame * Only carefully - it is hot!
    - Ah ah ah! He kicks!
    - Plant a fire bug on him - it will calm down instantly.
    - Class! Acts!
    - Ato! Now tell him:
    function throwException( ){
        (void 0)()
    }
    throwException()
    

    - He swears!
    - Namely, when he hears something incomprehensible, he begins to water you with stektrace. Now say:
    function throwError( ){
        throw new Error( 'error' )
    }
    throwError()
    - Something grunted and that’s it.
    - Yeah, here you see him too lazy to trisanut stack on you.
    - How then to be? Sometimes it’s necessary to say something like that ...
    - Let him come here ... * whisper a mantra in his ear *
    var Error= new function( Original ){

    Version: 1
    Description: "repairs mozilla's stacktrace in custom errors"
    License: 'public domain'

    Implementation:

    var Error= ( window.console && Original && ( new Original ).stack )
    ?  function( message ){
            var e= new Original( message )
            var custom= function( msg, file, line ){
                restore()
                if( other && other.apply( this, arguments ) ) return true
                if(( e.message != msg )||( e.fileName != file )||( e.lineNumber != line )) return
                var stack= e.stack.split( '\n' )
                stack= stack.slice( 2, stack.length - 1 )
                stack[0].replace( /^.*\(.+?\)@(.+):(\d+)$/, function( str, file, line ){
                    e.fileName= file
                    e.lineNumber= Number( line )
                })
                stack[ stack.length - 1 ]= stack[ stack.length - 1 ].replace( /^@.+:\d+$/, function( str ){
                    return 'source file()' + str
                })
                e.stack= stack.join( '\n' )
                console.error( e )
                return true
            }
            var other= window.onerror
            var restore= function(){
                if( window.onerror === custom ) window.onerror= other
            }
            window.onerror= custom
            setTimeout( restore, 1 )
            return e
        }
    :  Original

    Export: return Error

    Usage: throw new Error( 'custom error' )

    }( window.Error )
    “What are you whispering to him there?”
    - Never mind. Repeat now to him what he said earlier.
    - Nothing! Here it is traysanul so traysanul! What's the secret?
    - Ha! This is a special, street magic! Let's just say: I was able to convince him that stackrace is very important to us.
    “M ... okay, I see, what does the disappearance of the boys have to do with it?”
    - Ah ... yes ... let's go to Uncle Chrome, my little Stack, he will explain everything ...

    Also popular now: