Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ReactCommon/cxxreact/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ rn_xplat_cxx_library(

CXXREACT_PUBLIC_HEADERS = [
"CxxNativeModule.h",
"ErrorUtils.h",
"Instance.h",
"JSBundleType.h",
"JSDeltaBundleClient.h",
Expand Down
35 changes: 35 additions & 0 deletions ReactCommon/cxxreact/ErrorUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <jsi/jsi.h>

namespace facebook {
namespace react {

inline static void handleJSError(
jsi::Runtime &runtime,
const jsi::JSError &error) {
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 " +
error.getMessage(),
error.getStack());
}
// 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, error.value(), jsi::Value(true));
}

} // namespace react
} // namespace facebook
49 changes: 19 additions & 30 deletions ReactCommon/cxxreact/NativeToJsBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <jsi/jsi.h>
#include <reactperflogger/BridgeNativeModulePerfLogger.h>

#include "ErrorUtils.h"
#include "Instance.h"
#include "JSBigString.h"
#include "MessageQueueThread.h"
Expand All @@ -26,6 +27,8 @@

#ifdef WITH_FBSYSTRACE
#include <fbsystrace.h>
#include <jsi/jsi/jsi.h>

using fbsystrace::FbSystraceAsyncFlow;
#endif

Expand Down Expand Up @@ -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;
}

Expand Down