OpenFL UncaughtErrorEvent can be cached only in main app class?

Hi.
Added loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onError); to Main app class.
If I call throw haxe.io.Error.Custom("ERROR!"); in Main class, handler invoked.
But if I call throw haxe.io.Error.Custom("ERROR!"); in any other (not displayobject) class, handler hasn’t been invoked.

I believe we try/catch around every event dispatch from the stage, so if you throw the error in Event.ENTER_FRAME code I believe it will work, but I’m not sure it will work if you throw it from a Haxe timer (for example)

Well it doesn’t work as expected (like it worked in Flash for ex).
handler is not beinge invoked, however I can see error in console.
I’ll try to add some example later.

UPD:
Simple sample on document class

        loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onError);

		openfl.Lib.application.window.stage.addEventListener(openfl.events.MouseEvent.MOUSE_DOWN, e -> {
			var a:Dynamic = null;
			a.b = 1;
		});

...

        private function onError(event:UncaughtErrorEvent):Void
        {
		      Browser.alert("pizdec");
        }

@singmajesty I’ve created another simple sample, that doesn’t work. Can you help me please?

class Main extends openfl.display.Sprite
{
	public function new()
	{
		super();

		loaderInfo.uncaughtErrorEvents.addEventListener(openfl.events.UncaughtErrorEvent.UNCAUGHT_ERROR, handleUncaughtError);
		stage.addEventListener(openfl.events.MouseEvent.CLICK, event -> throw new openfl.errors.Error("ERROR!"));
	}

	private function handleUncaughtError(event:openfl.events.UncaughtErrorEvent):Void
	{
		js.Browser.alert("ERROR!");
	}
}

Using temporary workaround for HTML5 target:

js.Browser.window.onerror = function(msg, url, line, col, error) {
	// Note that col & error are new to the HTML 5 spec and may not be
	// supported in every browser.  It worked for me in Chrome.
	var extra = (col == 0 ? '' : '\ncolumn: ' + col);
	untyped extra += !error ? '' : '\nerror: ' + error.stack;

	// You can view the information in an alert to see things working like this:
	js.Browser.alert("Error: " + msg + "\nurl: " + url + "\nline: " + line + extra);

	var suppressErrorAlert = true;
	// If you return true, then error alerts (like in older versions of
	// Internet Explorer) will be suppressed.
	return suppressErrorAlert;
};

UPD:
Another solution:

var captureStack = (cast js.lib.Error).captureStackTrace;
(cast js.lib.Error).captureStackTrace = function(a, b):Void
{
	captureStack(a, b);
	js.Browser.alert(a.stack);
}
1 Like