Skip to content

Commit

Permalink
provide default implementation for ReactHostDelegate.handleInstanceEx…
Browse files Browse the repository at this point in the history
…ception()

Summary:
[ReactHostDelegate.handleInstanceException()](https://github.com/facebook/react-native/blob/a6f5e5adebed3d9da411f99548e2d8ce96636e16/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/defaults/DefaultReactHostDelegate.kt#L48) is a no-op so providing a default implementation for this.
As a simplified solution, just throw a `RuntimeException` (on non-debug bulid) in this case. Below is the justification.

1) We may want to consider using `ExceptionsManagerModule` TurboModule in OSS to report the exception but current implementation  just [throws JavaScriptException](https://github.com/facebook/react-native/blob/a6f5e5adebed3d9da411f99548e2d8ce96636e16/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/ExceptionsManagerModule.java#L67) when it is fatal

2) If the exception happens during ReactInstance initialization then we may not have Turbo Module initialized and may not be able to access it. While we might want to fix how this works in longer term but for now just throw an Exception and let the app handle it.

3) In debug build, `DevSupportManager` is called before `handleInstanceException()` which would display a RedBox so don't throw when it is a debug build.

So it seems to best for now to just throw an exception so it can be handled by the app than silently ignoring it.

Changelog:
[General][Android] - provide default implementation for ReactHostDelegate.handleInstanceException()

Differential Revision: D59847543
  • Loading branch information
alanleedev authored and facebook-github-bot committed Jul 18, 2024
1 parent a37534e commit 55c151a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.facebook.react.ReactPackage
import com.facebook.react.ReactPackageTurboModuleManagerDelegate
import com.facebook.react.bridge.JSBundleLoader
import com.facebook.react.common.annotations.UnstableReactNativeAPI
import com.facebook.react.common.build.ReactBuildConfig
import com.facebook.react.fabric.ReactNativeConfig
import com.facebook.react.runtime.BindingsInstaller
import com.facebook.react.runtime.JSRuntimeFactory
Expand Down Expand Up @@ -45,7 +46,11 @@ public class DefaultReactHostDelegate(
override val jsRuntimeFactory: JSRuntimeFactory = HermesInstance(),
override val bindingsInstaller: BindingsInstaller? = null,
private val reactNativeConfig: ReactNativeConfig = ReactNativeConfig.DEFAULT_CONFIG,
private val exceptionHandler: (Exception) -> Unit = {},
private val exceptionHandler: (Exception) -> Unit = {
if (!ReactBuildConfig.DEBUG) {
throw RuntimeException("Unrecoverable error occurred while running React Native!", it)
}
},
override val turboModuleManagerDelegateBuilder: ReactPackageTurboModuleManagerDelegate.Builder
) : ReactHostDelegate {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,11 @@ private Task<Void> getOrCreateStartTask() {
.continueWithTask(
(task) -> {
if (task.isFaulted()) {
mReactHostDelegate.handleInstanceException(task.getError());
Exception ex = task.getError();
if (mUseDevSupport) {
mDevSupportManager.handleException(ex);
}
mReactHostDelegate.handleInstanceException(ex);
// Wait for destroy to finish
return getOrCreateDestroyTask(
"getOrCreateStartTask() failure: " + task.getError().getMessage(),
Expand Down

0 comments on commit 55c151a

Please sign in to comment.