View Full Version : Exception handling

25 Aug 2011, 9:20 AM
I want to handle exceptions in an ExtJS app, gracefully. For Ajax requests (whether through a proxy or via Ext.Ajax), I want to install a global handler that listens for exceptions, but I only want it to get called if the exception is not handled in code. For example, if I add an exception listener to a proxy, I want to be able to indicate whether or not that handler has responded to an error condition (perhaps by returning true/false) so that the global handler gets invoked only if the listener doesn't handle it.

The problem is, it appears that the global handler gets called first, before any of my more localized handlers. Is there some way to achieve what I need? Are there any good references for how to deal with exceptions in ExtJS in general?

27 Aug 2011, 9:43 AM
One idea. The global exception handler for your Ajax requests could check the request options to see whether a failure handler is specified. It would then only process the exception if there is no failure handler. If the failure handler subsequently decides it can't handle the exception it can always call out to your generic error handler.

Are there any good references for how to deal with exceptions in ExtJS in general?

Not sure what you mean by this. Are you referring to exceptions as the term is used in Java? If so, that paradigm doesn't translate to JavaScript. JavaScript errors map most closely onto Java's unchecked exceptions in that they almost always signify a coding error. It is possible to catch all errors (http://docs.sencha.com/ext-js/4-0/#!/api/Ext.supports-property-WindowOnError) but graceful error handling is easier said than done as the app may well be in some sort of inconsistent state by that point. Any attempt at error handling may just cause another error.

28 Aug 2011, 4:50 AM
May I ask how do you register the global exception handler?

28 Aug 2011, 9:01 AM
Ext.Ajax has a requestexception event. You could define this globally the following way:

Ext.Ajax.on('requestexception', function(connection, response, options) {
//your code