requirejs does not clean up properly before calling onError #156

Open
kensaggy opened this Issue Dec 30, 2011 · 1 comment

Projects

None yet

2 participants

@kensaggy

When an error occurs, requirejs does not clean up properly before calling onError, and so when calling require() inside an onError handler nothing happens.

example code:

require.onError = function(err) {
    console.log(err); //temporary until error handler loads (or if it fails too)
}

//main.js
require(['errorhandler'] ,function(errorhandler) { 
        console.log('done'); 
        require.onError = errorhandler; 
}); 

require(['require','domReady','app'], function(require, domReady, App) 
{ 
        domReady(function() { 
                console.log('DOM IS READY! GO!'); 
                App.initialize(); 
        }); 
});  


//errorhandler.js 
define(['require'],  function () { 
        return (function(errObject) { 
                function handleTimeouts(modules) { 
                        for ( k in modules ) { 
                                switch (modules[k]) { 
                                        case "jquery": { 
                                                console.log('It seems jquery timedout, lets fall back to local 
copy'); 
                                                require(['libs/jquery/1.7.1/jquery-1.7.1'], function($) { // 
trying to load the local copy 
                                                        console.log('jquery has been loaded from local' , $.fn.jquery); 
                                                }); 
                                                break; 
                                        } 
                                        //Add more modules here to handler their fallback procedure. 
                                        default: { 
                                                console.log('I do not know this library [',modules[k],'] - can 
\'t do anything to recover'); 
                                        } 
                                } 
                        } 
                } 
                requireType = errObject.requireType; 
                requireModules = errObject.requireModules.trim().split(' '); 
                if (requireType == "timeout") { 
                        handleTimeouts(requireModules); 
                } 
        }); 
}); 

When this is ran (and i simulate a jquery 'require' failing to load, using a typo
in the url of the cdn version), the error handlers reaches up to the
"It seems jquery timedout," message, and stops there, never trying to
run that "require" statement that follows.

I think this is an important bug since it will allow developers to have somewhat of a fallback/recovery strategy on errors.

See discussion in google group at : http://groups.google.com/group/requirejs/browse_thread/thread/8063a649051dadbf/
As seen in the discussion thread, possible just make sure that require can be re-run and not take care of clean up/removing old script tags (the script tag that failed) at the moment

Thanks in advance,

Ken.

@fluidsonic

It also doesn't call $.holdReady(false) to allow the DOM ready event to occur, which may be needed by the error handler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment