Theo.T
Theo.T

Reputation: 9267

RequireJS: finding the script responsible for an error

I'm looking for an elegant way to find out the full path to a script that caused a timeout error (i.e. failed to load a dependency).

requirejs.onError = function (err) {

   // this works:
   var script_that_failed_loading = err.originalError.target.src

   // now I want:
   var the_script_responsible_for_this = <???>

};

Upvotes: 17

Views: 3288

Answers (2)

user2396441
user2396441

Reputation:

Use loader level errorbacks

require(["foo","bar"],function(foo,bar){
  // perform some action
},function(error){
  // handle error here
});

Note that failed module name is given in error.requireModules. Such errorbacks can be used both for loaders and modules. If you have multiple fallback paths for a resource, use path fallbacks.

As per my personal experience, I humbly disagree with ddotsenko. We're using RequireJS in our production environment. If setup properly, RJS is very reliable.

Upvotes: 8

ddotsenko
ddotsenko

Reputation: 4996

RequireJS chose a somewhat unreliable, disconnected mechanism for catching errors. It uses a timer to see if what it expected to get it got. Some other AMD loaders use other, more direct mechanisms to detect error conditions during loading.

My preferred AMD loader is CurlJS, which is hard-wired to catch the loading / parsing errors.

Because of the architectural choice it is more reliably detecting error conditions and you can attach error handlers directly to each require call. If stuff like error catching is important to you, I strongly suggest looking at CurlJS.

Upvotes: 2

Related Questions