Global Error Handling in Flex
January 4th, 2010 Brian Gray, Consultant
I have worked on a few large Flex applications, and almost everything about the platform delights me — it is quick to prototype, powerful in creating rich UIs. But it often frustrates me how difficult it is to handle run-time errors. As the application scales, it becomes harder and harder to ensure that it will never emit a run-time error, and the default Flash Player behavior of freezing and not providing any user feedback will not cut the mustard. The approach I have found to work best is as follows (until a future beta of Flash Player 10 supports this out of the box). Jörg Birkhold describes how to tell the call later dispatcher to throw an event any time an error is thrown.
private function onPreinitialize():void {
// setup global error handling
UIComponentGlobals.catchCallLaterExceptions = true;
systemManager.addEventListener("callLaterError", handleErrors);
}
I add an event handler in my Main application, doing the following:
- Print the stack trace to the console for debugging, since it will no longer print by default
- Pop an alert up to the user. Depending on how the error is thrown, this may or may not make it to the screen, but it will ensure the screen glosses over to prevent any further interaction.
- If JavaScript is available, call a JavaScript function. You can do anything you want here, but my preference is to redirect the user to an error page that tells them something went wrong, and links them to the correct page to restart where they left off.
public function handleErrors(event:Event):void {
if (event is DynamicEvent && event.hasOwnProperty("error")) {
// you could also send this back to your server
var error:Error = DynamicEvent(event).error as Error;
trace("Error!\n" + error.getStackTrace());
Alert.show("There has been an error in the application", "", Alert.OK);
if (ExternalInterface.available) {
ExternalInterface.call("handleApplicationError");
}
}
}
Download the full demo. To create this project in Flex Builder: Create a new Flex Project. Right click and select Import, select Archive File, select this file, and import all contents.
Entry Filed under: Software Development






4 Comments Add your own
1. chandra shekhar | January 9th, 2010 at 5:05 am
Nice article.
“Download the full demo” link not working
2. Brian Gray | March 3rd, 2010 at 5:22 pm
@chandra: Thanks! Seems like the link is working now. Let me know if you still have trouble with it.
3. Agraj | May 31st, 2010 at 6:03 am
Hi Brian,
Thanks, I was looking for exactly the same thing.
However, this technique is not working in my project and I have no wild idea why is it so ?
In case of an error, the control never reaches the error handler.
Any ideas ?
Regards
4. Brian Gray | June 2nd, 2010 at 4:52 pm
Hi Agraj,
Are you able to run the demo to get the desired effect?
It may be an issue with where the errors are originating from. This post finds that if an error is thrown by an event dispatcher, there is no way to catch it on the initiating thread. That is why, in my demo, I use this click handler:
click="callLater(throwError)"Instead of:
click="throwError()"Typically in a large MVC application, this is not an issue, as the handlers are very simple and just dispatch events. Most frameworks (Mate, Cairngorm, etc.) encourage this pattern. However there are certainly times where this is not the case and care has to be taken to ensure that the exception code will be in the callLater queue.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed