Error handling for the ESRI JS API
dojo.publish(Topic Name [string], Arguments to Pass to Subscribed Function [array])
handle = dojo.subscribe(Topic Name [string], Context of Linked Method [string or null], Linked Method [string or function])
dojo.unsubscribe(Handle [handle object])
Well, the JS API leverages this and publishes an event named ‘esri.Error‘ whenever an error happens during an ajax request. So, you can subscribe to the ‘esri.Error’ event that is published by the JS API right before calling execute on the QueryTask and unsubscribe when the results are successfully returned or if an error occurred. This should suffice for most cases but if you are a stickler, you will soon realize that if you execute many QueryTasks simultaneously the error subscription only lets you know that an error occurred and the error message. It however does not let you know which one of the QueryTasks failed. Another way the error can be handled is by temporarily overwriting the ‘esriConfig.defaults.io.errorHandler‘ function and resetting it after the request has returned successfully or with an error. But this way also has similar drawbacks. The best solution to the problem would be for the execute method on the QueryTask to take an error handler function as another parameter.