Don’t cast an UncaughtErrorEvent error

I just found this out the hard way, but you shouldn’t cast the error property of an UncaughtErrorEvent. I was doing the following in my uncaught error event handler:

1
2
3
4
5
6
7
8
9
private function onUncaughtErrorEvent(event:UncaughtErrorEvent):void {
	event.preventDefault();
	event.stopImmediatePropagation();
	if(event.error is Error) {
		log(Error(event.error).getStackTrace());
	}else if (event.error is ErrorEvent) {
		log(ErrorEvent(event.error).text);
	}
}

The problem with this is that it returns your uncaught error event handler as the last step when you call getStackTrace(). But changing your code a little will give you the real source of your error. Maybe everyone else knew this already, but I’m logging it here on my blog so I don’t forget.

1
2
3
4
5
6
7
8
9
private function onUncaughtErrorEvent(event:UncaughtErrorEvent):void {
	event.preventDefault();
	event.stopImmediatePropagation();
	if(event.error is Error) {
		log((event.error as Error).getStackTrace());
	}else if (event.error is ErrorEvent) {
		log(ErrorEvent(event.error).text);
	}
}

One thought on “Don’t cast an UncaughtErrorEvent error