Destroy React Native instance after catching a fatal js error

Summary:
As title, destroy React Native instance after catching a fatal js error to avoid incoming calls into JS.

Changelog:
[Android][Changed] - Rename NativeModuleCallExceptionHandler to JSExceptionHandler for broader usage

Reviewed By: fkgozali

Differential Revision: D37379340

fbshipit-source-id: 465a30bc824a264b45df3e8b0b24edd61c4b571d
This commit is contained in:
Lulu Wu
2022-06-28 15:24:13 -07:00
committed by Facebook GitHub Bot
parent ca8481bd7d
commit b6f7689d70
19 changed files with 76 additions and 85 deletions
@@ -60,8 +60,8 @@ public class ReactContext extends ContextWrapper {
private @Nullable MessageQueueThread mUiMessageQueueThread;
private @Nullable MessageQueueThread mNativeModulesMessageQueueThread;
private @Nullable MessageQueueThread mJSMessageQueueThread;
private @Nullable NativeModuleCallExceptionHandler mNativeModuleCallExceptionHandler;
private @Nullable NativeModuleCallExceptionHandler mExceptionHandlerWrapper;
private @Nullable JSExceptionHandler mJSExceptionHandler;
private @Nullable JSExceptionHandler mExceptionHandlerWrapper;
private @Nullable WeakReference<Activity> mCurrentActivity;
private boolean mIsInitialized = false;
@@ -123,9 +123,8 @@ public class ReactContext extends ContextWrapper {
}
}
public void setNativeModuleCallExceptionHandler(
@Nullable NativeModuleCallExceptionHandler nativeModuleCallExceptionHandler) {
mNativeModuleCallExceptionHandler = nativeModuleCallExceptionHandler;
public void setJSExceptionHandler(@Nullable JSExceptionHandler jSExceptionHandler) {
mJSExceptionHandler = jSExceptionHandler;
}
private void raiseCatalystInstanceMissingException() {
@@ -406,18 +405,17 @@ public class ReactContext extends ContextWrapper {
}
/**
* Passes the given exception to the current {@link
* com.facebook.react.bridge.NativeModuleCallExceptionHandler} if one exists, rethrowing
* Passes the given exception to the current {@link JSExceptionHandler} if one exists, rethrowing
* otherwise.
*/
public void handleException(Exception e) {
boolean catalystInstanceVariableExists = mCatalystInstance != null;
boolean isCatalystInstanceAlive =
catalystInstanceVariableExists && !mCatalystInstance.isDestroyed();
boolean hasExceptionHandler = mNativeModuleCallExceptionHandler != null;
boolean hasExceptionHandler = mJSExceptionHandler != null;
if (isCatalystInstanceAlive && hasExceptionHandler) {
mNativeModuleCallExceptionHandler.handleException(e);
mJSExceptionHandler.handleException(e);
} else {
FLog.e(
ReactConstants.TAG,
@@ -432,14 +430,14 @@ public class ReactContext extends ContextWrapper {
}
}
public class ExceptionHandlerWrapper implements NativeModuleCallExceptionHandler {
public class ExceptionHandlerWrapper implements JSExceptionHandler {
@Override
public void handleException(Exception e) {
ReactContext.this.handleException(e);
}
}
public NativeModuleCallExceptionHandler getExceptionHandler() {
public JSExceptionHandler getExceptionHandler() {
if (mExceptionHandlerWrapper == null) {
mExceptionHandlerWrapper = new ExceptionHandlerWrapper();
}