Results 1 to 2 of 2

Thread: Global error handling

  1. #1
    Sencha - Ext JS Dev Team
    Join Date
    Dec 2009
    Location
    Iasi, Romania
    Posts
    130
    Vote Rating
    24
      0  

    Default Global error handling

    Hi,

    I believe that in any Ext JS application you need some kind of global error handling to be able to improve your app constantly. If you would have something like this in place you could send bug reports automatically.
    This could be achieved with the window.onerror but this one lacks a stack in some browsers. A bug report without an error stack is incomplete. That's why I came with the following approach which so far is working pretty nice in my app.

    Code:
    Ext.ClassManager.create = Ext.Function.createInterceptor(Ext.ClassManager.create, function(className, data, createdFn){
        // let's add a property available for all classes
        // we need this to be able to run our own postprocessor during class "define"
        if(!data.hasOwnProperty('handleGlobalErrors')){
            data['handleGlobalErrors'] = true;
        }
    });
    Ext.ClassManager.registerPostprocessor('handleGlobalErrors', function(processor, cls, data, fn){
        var forbiddenClasses = ['Ext.util.Observable', 'Ext.Base'],
            forbidden = ['self'],
            newMethods = {};
    
    
        if(!data['handleGlobalErrors']){
            return;
        }
        
        // overriding some standard methods could cause problems and we want to avoid that
        Ext.each(forbiddenClasses, function(className){
            var c = Ext.ClassManager.get(className);
            
            Ext.Object.each(c, function(name, method){
                if(Ext.isFunction(method)){
                    forbidden.push(name);
                }
            });
        });
        
        Ext.Object.each(cls.prototype, function(name, method){
            if(Ext.isFunction(method) && Ext.Array.indexOf(forbidden, name) < 0){
                newMethods[name] = function(name, method){
                    return function(){
                        try {
                            return method.apply(this, arguments);
                        } catch (ex) {
                            // implement here a global error handler which can either pop up the error or send it to our server
                            try {
                                Ext.Msg.alert('Error', ex.message);
                                Ext.error('New error caught: %s, %o', cls.$className + ':' + name + "(): " + ex.message, ex.stack);
                            } catch (ex) {
                                Ext.log(cls.$className + ':' + name + "(): " + ex.message);
                            }
                        }
                    };
    
    
                }(name, method);
            }
        });
        Ext.override(cls, newMethods);
    });
    When overriding some methods you could run in some problems, that's why I keep a list of forbidden classes methods.

    You need to put this code before the definition of the Application class. This way any class that you define in your app will have this inside. If you don't want to handle the errors inside a specific class, you could set the config "handleGlobalError" to false.

    Please let me know your thoughts on this approach.

    Cheers,
    Adrian

  2. #2
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    9,390
    Vote Rating
    498
      0  

    Default

    Thank you for sharing your code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •