Move error handling with JSI into a separate helper function (#29090)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/29090

Moving the logic for calling into JS to handle errors into ErrorUtils, where it can be reused outside of the bridge.

Reviewed By: RSNara

Differential Revision: D21939254

fbshipit-source-id: 0d8f3bd2503720be7619ed8dc8b2389f544049f3
This commit is contained in:
Emily Janzer
2020-06-10 10:31:46 -07:00
committed by Facebook GitHub Bot
parent 02b8e67da3
commit 92630856c6
3 changed files with 55 additions and 30 deletions
+19 -30
View File
@@ -14,6 +14,7 @@
#include <jsi/jsi.h>
#include <reactperflogger/BridgeNativeModulePerfLogger.h>
#include "ErrorUtils.h"
#include "Instance.h"
#include "JSBigString.h"
#include "MessageQueueThread.h"
@@ -26,6 +27,8 @@
#ifdef WITH_FBSYSTRACE
#include <fbsystrace.h>
#include <jsi/jsi/jsi.h>
using fbsystrace::FbSystraceAsyncFlow;
#endif
@@ -338,37 +341,23 @@ std::shared_ptr<CallInvoker> NativeToJsBridge::getDecoratedNativeCallInvoker(
}
RuntimeExecutor NativeToJsBridge::getRuntimeExecutor() {
auto runtimeExecutor = [this, isDestroyed = m_destroyed](
std::function<void(jsi::Runtime & runtime)>
&&callback) {
if (*isDestroyed) {
return;
}
runOnExecutorQueue([callback = std::move(callback)](JSExecutor *executor) {
jsi::Runtime *runtime = (jsi::Runtime *)executor->getJavaScriptContext();
try {
callback(*runtime);
} catch (jsi::JSError &originalError) {
auto errorUtils = runtime->global().getProperty(*runtime, "ErrorUtils");
if (errorUtils.isUndefined() || !errorUtils.isObject() ||
!errorUtils.getObject(*runtime).hasProperty(
*runtime, "reportFatalError")) {
// ErrorUtils was not set up. This probably means the bundle didn't
// load properly.
throw jsi::JSError(
*runtime,
"ErrorUtils is not set up properly. Something probably went wrong trying to load the JS bundle. Trying to report error " +
originalError.getMessage(),
originalError.getStack());
auto runtimeExecutor =
[this, isDestroyed = m_destroyed](
std::function<void(jsi::Runtime & runtime)> &&callback) {
if (*isDestroyed) {
return;
}
// TODO(janzer): Rewrite this function to return the processed error
// instead of just reporting it through the native module
auto func = errorUtils.asObject(*runtime).getPropertyAsFunction(
*runtime, "reportFatalError");
func.call(*runtime, originalError.value(), jsi::Value(true));
}
});
};
runOnExecutorQueue(
[callback = std::move(callback)](JSExecutor *executor) {
jsi::Runtime *runtime =
(jsi::Runtime *)executor->getJavaScriptContext();
try {
callback(*runtime);
} catch (jsi::JSError &originalError) {
handleJSError(*runtime, originalError);
}
});
};
return runtimeExecutor;
}