Skip to content

Commit

Permalink
Wrap NullPointerExceptions when thrown by native code called from JS
Browse files Browse the repository at this point in the history
  • Loading branch information
hsource committed Jun 26, 2023
1 parent 26ef0d5 commit be44f39
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,23 +370,29 @@ public void invoke(JSInstance jsInstance, ReadableArray parameters) {

try {
mMethod.invoke(mModuleWrapper.getModule(), mArguments);
} catch (IllegalArgumentException ie) {
throw new RuntimeException("Could not invoke " + traceName, ie);
} catch (IllegalAccessException iae) {
throw new RuntimeException("Could not invoke " + traceName, iae);
} catch (IllegalArgumentException | IllegalAccessException | NullPointerException e) {
throw new RuntimeException(createInvokeExceptionMessage(traceName, parameters), e);
} catch (InvocationTargetException ite) {
// Exceptions thrown from native module calls end up wrapped in InvocationTargetException
// which just make traces harder to read and bump out useful information
if (ite.getCause() instanceof RuntimeException) {
throw (RuntimeException) ite.getCause();
}
throw new RuntimeException("Could not invoke " + traceName, ite);
throw new RuntimeException(createInvokeExceptionMessage(traceName, parameters), ite);
}
} finally {
SystraceMessage.endSection(TRACE_TAG_REACT_JAVA_BRIDGE).flush();
}
}

/**
* Makes it easier to determine the cause of an error invoking a native method from Javascript
* code by adding the function and parameters.
*/
private static String createInvokeExceptionMessage(String traceName, ReadableArray parameters) {
return "Could not invoke " + traceName + " with parameters " + parameters.toArrayList();
}

/**
* Determines how the method is exported in JavaScript: METHOD_TYPE_ASYNC for regular methods
* METHOD_TYPE_PROMISE for methods that return a promise object to the caller. METHOD_TYPE_SYNC
Expand Down

0 comments on commit be44f39

Please sign in to comment.