How to handle 404 errors from Requirejs

Viewed 3923

I am currently trying to identify the best way to handle different errors within a Durandal application. One scenario I am testing is where the required module does not exist, i.e.

define(['dummy'], function (Dummy) { return {}; });

where dummy does not exist. I have added the following to my main.js file directly below requirejs.config({...}):

requirejs.onError = function (err) {
    console.log('Global error', err);
};

but the error is never logged from here. The 404 error is being shown in the console window from Durandals system.js file and the error message is also logged:

Uncaught Error: Failed to load routed module (viewmodels/features/errorHandling/scriptNotFound/one). Details: Script error for: dummy

It looks like the requirejs.onError function never gets a chance to be called. I would like to be able to log these errors and inform the user that there was a problem etc. Anybody any idea how I would do this?

5 Answers
Related