1. #1
    Sencha Premium Member
    Join Date
    Dec 2009
    Location
    Iasi, Romania
    Posts
    84
    Vote Rating
    9
    ateodorescu will become famous soon enough

      0  

    Default Global error handling

    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
    https://github.com/ateodorescu/mzExt
    http://www.mzsolutions.eu/

    Mz.pivot.Grid
    Ext.ux.form.plugin.HtmlEditor
    Ext.ux.form.field.CodeMirror
    Ext.ux.form.field.ImageFileField
    Ext.ux.form.field.UploadFileField

  2. #2
    Sencha - Support Team scottmartin's Avatar
    Join Date
    Jul 2010
    Location
    Houston, Tx
    Posts
    8,872
    Vote Rating
    438
    scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future scottmartin has a brilliant future

      0  

    Default


    Thank you for sharing your code.

Thread Participants: 1

Tags for this Thread