Skip to content

Commit ff1972d

Browse files
hsourcefacebook-github-bot
authored andcommitted
Wrap NullPointerExceptions and throw new Error in dispatchViewManagerCommand for easier debugging (#38444)
Summary: This PR is a modified version of #38060 which was reverted because it may sometimes cause sensitive info to be logged. This version no longer includes any parameters in the thrown errors. ### Motivation I had a crash-causing error in our error reporting console (Bugsnag) that was extremely hard to debug due to the lack of specificity in the thrown error. ``` java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference at com.facebook.react.bridge.ReadableNativeArray.getDouble(ReadableNativeArray.java:92) at com.facebook.react.bridge.JavaMethodWrapper$4.extractArgument(JavaMethodWrapper.java:64) at com.facebook.react.bridge.JavaMethodWrapper$4.extractArgument(JavaMethodWrapper.java:60) at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:356) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:188) at com.facebook.jni.NativeRunnable.run(NativeRunnable.java:-2) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27) at android.os.Looper.loop(Looper.java:214) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:228) at java.lang.Thread.run(Thread.java:919) ``` I noticed that `invoke` in `JavaMethodWrapper` tacks on the JS method name on other errors, so wanted to change this to handle NullPointerExceptions more gracefully too. This helps make it easier to debug issues like #23126, #19413, #27633, #23378, #29250, #28262, #34001 and likely many more. ### After adding NullPointerException Even after adding the NullPointerException, I got errors like this: ``` com.facebook.react.bridge.NativeArgumentsParseException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference (constructing arguments for UIManager.dispatchViewManagerCommand at argument index 0) with parameters [null, 4.0, []] at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:371) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:188) at com.facebook.jni.NativeRunnable.run(NativeRunnable.java:-2) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:228) at java.lang.Thread.run(Thread.java:1012) ``` This helped, but still didn't help me easily find which library calling `dispatchViewManagerCommand` was causing this. ## Changelog: [ANDROID] [CHANGED] - Wrap NullPointerExceptions when thrown by native code called from JS for readability [GENERAL] [CHANGED] - Throw Error in dispatchViewManagerCommand when non-numeric tag is passed for easier debugging Pull Request resolved: #38444 Test Plan: Test change on our app via a beta release. With these changes, we got better stacktraces like these: ### Java stacktrace ``` com.facebook.react.bridge.NativeArgumentsParseException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference (constructing arguments for UIManager.dispatchViewManagerCommand at argument index 0) with parameters [null, 4.0, []] at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:371) at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:188) at com.facebook.jni.NativeRunnable.run(NativeRunnable.java:-2) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:228) at java.lang.Thread.run(Thread.java:1012) ``` ### JS stacktrace (after dispatchViewManagerCommand change) ``` Error dispatchViewManagerCommand: found null reactTag with args [] node_modules/react-native/Libraries/ReactNative/PaperUIManager.js:120:21 dispatchViewManagerCommand node_modules/react-native-maps/lib/MapMarker.js:68:67 _runCommand node_modules/react-native-maps/lib/MapMarker.js:60:24 redraw templates/Components/MapViewWithMarkers/PlaceMarker.tsx:88:32 anonymous (native) apply node_modules/react-native/Libraries/Core/Timers/JSTimers.js:213:22 anonymous node_modules/react-native/Libraries/Core/Timers/JSTimers.js:111:14 _callTimer node_modules/react-native/Libraries/Core/Timers/JSTimers.js:359:16 callTimers (native) apply node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:427:31 __callFunction node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:113:25 anonymous node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:368:10 __guard node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:112:16 callFunctionReturnFlushedQueue ``` Reviewed By: javache Differential Revision: D47546672 Pulled By: cortinico fbshipit-source-id: 68379561d84d0ef2ed5c6d66f3f49943c5d7cb7e
1 parent 5dce2e4 commit ff1972d

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

packages/react-native/Libraries/ReactNative/UIManager.js

+8
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ const UIManager = {
180180
commandName: number | string,
181181
commandArgs: any[],
182182
) {
183+
// Sometimes, libraries directly pass in the output of `findNodeHandle` to
184+
// this function without checking if it's null. This guards against that
185+
// case. We throw early here in Javascript so we can get a JS stacktrace
186+
// instead of a harder-to-debug native Java or Objective-C stacktrace.
187+
if (typeof reactTag !== 'number') {
188+
throw new Error('dispatchViewManagerCommand: found null reactTag');
189+
}
190+
183191
if (isFabricReactTag(reactTag)) {
184192
const FabricUIManager = nullthrows(getFabricUIManager());
185193
const shadowNode =

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/JavaMethodWrapper.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public void invoke(JSInstance jsInstance, ReadableArray parameters) {
356356
mArgumentExtractors[i].extractArgument(jsInstance, parameters, jsArgumentsConsumed);
357357
jsArgumentsConsumed += mArgumentExtractors[i].getJSArgumentsNeeded();
358358
}
359-
} catch (UnexpectedNativeTypeException e) {
359+
} catch (UnexpectedNativeTypeException | NullPointerException e) {
360360
throw new NativeArgumentsParseException(
361361
e.getMessage()
362362
+ " (constructing arguments for "
@@ -370,23 +370,29 @@ public void invoke(JSInstance jsInstance, ReadableArray parameters) {
370370

371371
try {
372372
mMethod.invoke(mModuleWrapper.getModule(), mArguments);
373-
} catch (IllegalArgumentException ie) {
374-
throw new RuntimeException("Could not invoke " + traceName, ie);
375-
} catch (IllegalAccessException iae) {
376-
throw new RuntimeException("Could not invoke " + traceName, iae);
373+
} catch (IllegalArgumentException | IllegalAccessException e) {
374+
throw new RuntimeException(createInvokeExceptionMessage(traceName), e);
377375
} catch (InvocationTargetException ite) {
378376
// Exceptions thrown from native module calls end up wrapped in InvocationTargetException
379377
// which just make traces harder to read and bump out useful information
380378
if (ite.getCause() instanceof RuntimeException) {
381379
throw (RuntimeException) ite.getCause();
382380
}
383-
throw new RuntimeException("Could not invoke " + traceName, ite);
381+
throw new RuntimeException(createInvokeExceptionMessage(traceName), ite);
384382
}
385383
} finally {
386384
SystraceMessage.endSection(TRACE_TAG_REACT_JAVA_BRIDGE).flush();
387385
}
388386
}
389387

388+
/**
389+
* Makes it easier to determine the cause of an error invoking a native method from Javascript
390+
* code by adding the function name.
391+
*/
392+
private static String createInvokeExceptionMessage(String traceName) {
393+
return "Could not invoke " + traceName;
394+
}
395+
390396
/**
391397
* Determines how the method is exported in JavaScript: METHOD_TYPE_ASYNC for regular methods
392398
* METHOD_TYPE_PROMISE for methods that return a promise object to the caller. METHOD_TYPE_SYNC

0 commit comments

Comments
 (0)