diff --git a/gwt-core-gwt2-tests/src/test/java/org/gwtproject/core/client/GWTTest.java b/gwt-core-gwt2-tests/src/test/java/org/gwtproject/core/client/GWTTest.java index 7b9a561..3d4151e 100644 --- a/gwt-core-gwt2-tests/src/test/java/org/gwtproject/core/client/GWTTest.java +++ b/gwt-core-gwt2-tests/src/test/java/org/gwtproject/core/client/GWTTest.java @@ -16,8 +16,14 @@ package org.gwtproject.core.client; import com.google.gwt.junit.client.GWTTestCase; +import elemental2.dom.DomGlobal; +import java.util.ArrayList; +import java.util.List; public class GWTTest extends GWTTestCase { + + private List caught = new ArrayList<>(); + @Override public String getModuleName() { return "org.gwtproject.core.Core"; @@ -32,4 +38,22 @@ public void testIsClient() { assertTrue(GWT.isClient()); assertTrue(org.gwtproject.core.shared.GWT.isClient()); } + + public void testReportUncaughtError() { + GWT.setUncaughtExceptionHandler(caught::add); + GWT.reportUncaughtException(new RuntimeException()); + DomGlobal.setTimeout( + (ignore) -> { + assertEquals(1, caught.size()); + assertEquals("java.lang.RuntimeException", caught.get(0).getClass().getName()); + finishTest(); + }, + 1000); + delayTestFinish(3000); + } + + @Override + public boolean catchExceptions() { + return false; + } } diff --git a/gwt-core/src/main/java/org/gwtproject/core/client/GWT.java b/gwt-core/src/main/java/org/gwtproject/core/client/GWT.java index d8fea32..6ac902a 100644 --- a/gwt-core/src/main/java/org/gwtproject/core/client/GWT.java +++ b/gwt-core/src/main/java/org/gwtproject/core/client/GWT.java @@ -17,6 +17,7 @@ import elemental2.dom.DomGlobal; import elemental2.dom.Window; +import jsinterop.annotations.JsFunction; import jsinterop.annotations.JsMethod; import jsinterop.annotations.JsType; import jsinterop.base.Js; @@ -58,11 +59,10 @@ public static T create(Class clazz) { public static void reportUncaughtException(Throwable e) { // throw an exception "later" so that it ends up handled by the global // error handler. Same code as in GWT2's Impl.reportToBrowser() - DomGlobal.setTimeout( - ignore -> { - throw_(e); - }, - 0); + setTimeout( + () -> { + throw e; + }); } /** @@ -152,7 +152,7 @@ private static void addOnErrorHandler(Window window, Window.OnerrorFn onerrorFn) @JsMethod private static native Throwable fromObject(Object obj) /*-{ //GWT2 impl using JSNI, see GWT.native.js for the j2cl impl - var throwable = @java.lang.Throwable::of(*)(obj); + return @java.lang.Throwable::of(*)(obj); }-*/; @JsType(isNative = true, name = "window", namespace = "") @@ -160,8 +160,13 @@ private static class InnerWindow { static Window window; } - @JsMethod(namespace = "", name = "throw") - private static native void throw_(Object object); + @JsFunction + private interface Throwing { + void run() throws Throwable; + } + + @JsMethod(namespace = "", name = "setTimeout") + private static native void setTimeout(Throwing throwingFunction); public static boolean isClient() { return org.gwtproject.core.shared.GWT.isClient();