Skip to content

Commit

Permalink
Add JS error handling logic to bridge's RuntimeExecutor
Browse files Browse the repository at this point in the history
Summary:
Call into our existing JS error handling logic from C++ using JSI in the RuntimeExecutor used by the bridge.

Changelog: [Internal]

Reviewed By: lunaleaps

Differential Revision: D21908523

fbshipit-source-id: ae41196443781b9f2673dcb7bbcb5b5aa8aa2528
  • Loading branch information
Emily Janzer authored and facebook-github-bot committed Jun 6, 2020
1 parent 058eeb4 commit 965635f
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions ReactCommon/cxxreact/NativeToJsBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,19 +337,37 @@ 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;
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());
}
runOnExecutorQueue(
[callback = std::move(callback)](JSExecutor *executor) {
jsi::Runtime *runtime =
(jsi::Runtime *)executor->getJavaScriptContext();
callback(*runtime);
});
};
// 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));
}
});
};
return runtimeExecutor;
}

Expand Down

0 comments on commit 965635f

Please sign in to comment.