From 568d2206f7d91dc91d18f2c76dcf2767beb48eae Mon Sep 17 00:00:00 2001 From: Luna Wei Date: Thu, 18 Jun 2020 16:56:18 -0700 Subject: [PATCH] Add non-fatal error handling Summary: ## Changelog: [Internal][Added] - Additional option to report non-fatal JS error Reviewed By: ejanzer Differential Revision: D22054406 fbshipit-source-id: 25e2a8d45f086173b09ee095331f94364cae4c1a --- ReactCommon/cxxreact/ErrorUtils.h | 23 ++++++++++++++++------- ReactCommon/cxxreact/NativeToJsBridge.cpp | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/ReactCommon/cxxreact/ErrorUtils.h b/ReactCommon/cxxreact/ErrorUtils.h index aebd3918629..3ab715b2efb 100644 --- a/ReactCommon/cxxreact/ErrorUtils.h +++ b/ReactCommon/cxxreact/ErrorUtils.h @@ -10,12 +10,12 @@ namespace facebook { namespace react { -inline static void handleJSError( - jsi::Runtime &runtime, - const jsi::JSError &error) { +inline static void +handleJSError(jsi::Runtime &runtime, const jsi::JSError &error, bool isFatal) { auto errorUtils = runtime.global().getProperty(runtime, "ErrorUtils"); if (errorUtils.isUndefined() || !errorUtils.isObject() || - !errorUtils.getObject(runtime).hasProperty(runtime, "reportFatalError")) { + !errorUtils.getObject(runtime).hasProperty(runtime, "reportFatalError") || + !errorUtils.getObject(runtime).hasProperty(runtime, "reportError")) { // ErrorUtils was not set up. This probably means the bundle didn't // load properly. throw jsi::JSError( @@ -24,11 +24,20 @@ inline static void handleJSError( 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)); + if (isFatal) { + auto func = errorUtils.asObject(runtime).getPropertyAsFunction( + runtime, "reportFatalError"); + + func.call(runtime, error.value()); + } else { + auto func = errorUtils.asObject(runtime).getPropertyAsFunction( + runtime, "reportError"); + + func.call(runtime, error.value()); + } } } // namespace react diff --git a/ReactCommon/cxxreact/NativeToJsBridge.cpp b/ReactCommon/cxxreact/NativeToJsBridge.cpp index 2729997c7cb..d0c13554f28 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.cpp +++ b/ReactCommon/cxxreact/NativeToJsBridge.cpp @@ -354,7 +354,7 @@ RuntimeExecutor NativeToJsBridge::getRuntimeExecutor() { try { callback(*runtime); } catch (jsi::JSError &originalError) { - handleJSError(*runtime, originalError); + handleJSError(*runtime, originalError, true); } }); };