Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw JS exception instead of Java wrapping object #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<Throwable> caught = new ArrayList<>();

@Override
public String getModuleName() {
return "org.gwtproject.core.Core";
Expand All @@ -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;
}
}
21 changes: 13 additions & 8 deletions gwt-core/src/main/java/org/gwtproject/core/client/GWT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,11 +59,10 @@ public static <T> 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;
});
}

/**
Expand Down Expand Up @@ -152,16 +152,21 @@ 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 = "<window>")
private static class InnerWindow {
static Window window;
}

@JsMethod(namespace = "<window>", name = "throw")
private static native void throw_(Object object);
@JsFunction
private interface Throwing {
void run() throws Throwable;
}

@JsMethod(namespace = "<window>", name = "setTimeout")
private static native void setTimeout(Throwing throwingFunction);

public static boolean isClient() {
return org.gwtproject.core.shared.GWT.isClient();
Expand Down
Loading