When I try to load nonexistent module, it fail with 404 error (of course). I want to handle this error but don't know how to connect to "error" event.
According to Dojo doc, I should be able to do that using its micro event api.
This code does not work.
var handle = require.on('error', function( error )
{
alert('Finally error')
});
require(['nonexistent/module'], function ( m )
{
alert('Module was loaded correctly')
});
Dojo version is 1.7.1, browser latest Chrome.
When I try to load nonexistent module, it fail with 404 error (of course). I want to handle this error but don't know how to connect to "error" event.
According to Dojo doc, I should be able to do that using its micro event api.
This code does not work.
var handle = require.on('error', function( error )
{
alert('Finally error')
});
require(['nonexistent/module'], function ( m )
{
alert('Module was loaded correctly')
});
Dojo version is 1.7.1, browser latest Chrome.
Share Improve this question edited May 4, 2012 at 13:00 Peter asked May 4, 2012 at 12:54 PeterPeter 9141 gold badge15 silver badges27 bronze badges 1-
A
require.on
handler like this is called for every error event after that point in time, right? I have usedvar h = require.on('error', function (err) { h.remove(); ...etc }
but this will only handle the first error event, and not necessarily the one caused by thenonexistent/module
. Any more graceful way to only handle the error for your module, short of searching the error string for your module name? – Neek Commented Sep 20, 2016 at 9:12
2 Answers
Reset to default 5The documentation on this one is a bit curious. I can get the 'error' event to fire after a short timeout (dojoConfig.waitSeconds) after a failed xhr require, but only in async mode, and only with a dev build of Dojo.
Why? Well, only async mode raises the event, and it looks like the CDN build was built with dojo-timeout-api == 0.
Can you use your own custom (SDK) version of Dojo?
@mschr - your jsfiddle sample works if you use Dojo (nightly) instead of Dojo 1.7.2 - same reason as above.
try it with debugging set
<script src="{{baseUrl}}/dojo/dojo.js" data-dojo-config="async:true, debug: true, debugAtAllCost:true"></script>
Edit; no such luck - it seems that docs are ahead of implementation here..
created a fiddle for the purpose of maybe finding a solution, concept is nice, would enjoy to use this myself
http://jsfiddle/seeds/h6bXQ/