Skip to content

Commit

Permalink
[TIMOB-24657] Allow native exceptions to bubble up into JS
Browse files Browse the repository at this point in the history
  • Loading branch information
janvennemann authored and sgtcoolguy committed Nov 16, 2017
1 parent 4e96624 commit 5d54980
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
24 changes: 16 additions & 8 deletions android/src/hyperloop/BaseProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private KrollDict argsToDict(Object[] args) {
}

@Kroll.method
public Object callNativeFunction(Object[] args) {
public Object callNativeFunction(Object[] args) throws Exception {
KrollDict dict = argsToDict(args);

String methodname = dict.getString("func");
Expand All @@ -124,7 +124,19 @@ public Object callNativeFunction(Object[] args) {
return null;
}
Object receiver = (isInstanceMethod ? getReceiver() : null);
Object result = invokeMethod(m, receiver, convertedArgs);
Object result = null;
try {
result = invokeMethod(m, receiver, convertedArgs);
} catch(InvocationTargetException e) {
if (e.getCause() instanceof Exception) {
throw (Exception)e.getCause();
} else {
Log.e(TAG, "Error thrown during invocation of method: " + m.toString()
+ ", args: "
+ Arrays.toString(convertedArgs),
e.getCause());
}
}
if (result == null) {
return result;
}
Expand All @@ -138,7 +150,7 @@ private Method findMethod(String methodName, Object[] convertedArgs,
return HyperloopUtil.resolveMethod(clazz, methodName, convertedArgs, instanceMethod);
}

protected Object invokeMethod(Method m, Object receiver, Object[] convertedArgs) {
protected Object invokeMethod(Method m, Object receiver, Object[] convertedArgs) throws InvocationTargetException {
m.setAccessible(true); // should offer perf boost since doesn't have to
// check security
try {
Expand All @@ -149,12 +161,8 @@ protected Object invokeMethod(Method m, Object receiver, Object[] convertedArgs)
} catch (IllegalArgumentException e) {
Log.e(TAG, "Bad argument for method: " + m.toString() + ", args: "
+ Arrays.toString(convertedArgs), e);
} catch (InvocationTargetException e) {
Log.e(TAG, "Exception thrown during invocation of method: " + m.toString()
+ ", args: "
+ Arrays.toString(convertedArgs),
e.getCause());
}

return null;
}

Expand Down
3 changes: 2 additions & 1 deletion android/src/hyperloop/InstanceProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

package hyperloop;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

Expand Down Expand Up @@ -174,7 +175,7 @@ public void release() {
}

@Override
protected Object invokeMethod(Method m, Object receiver, Object[] convertedArgs) {
protected Object invokeMethod(Method m, Object receiver, Object[] convertedArgs) throws InvocationTargetException {
if (receiver != null && isSuper) {
// let the handler know this call is explicitly to super!
HyperloopInvocationHandler hih = (HyperloopInvocationHandler) ProxyBuilder.getInvocationHandler(receiver);
Expand Down

0 comments on commit 5d54980

Please sign in to comment.