Skip to content

Commit f4b0fcb

Browse files
huzhanbo1996Titozzz
authored andcommitted
fix: android native rejections should be instanceof Error (#44487)
Summary: fix #44050 ## Changelog: [ANDROID] [FIXED] - fix: android native rejections should be instanceof Error Pull Request resolved: #44487 Test Plan: reject returns Error. Reviewed By: NickGerleman Differential Revision: D57205131 Pulled By: javache fbshipit-source-id: a5950481d0c4909be4dbea0b430e75222258ae68
1 parent e210c7c commit f4b0fcb

1 file changed

Lines changed: 53 additions & 10 deletions

File tree

  • packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon

packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.cpp

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,38 @@ struct JNIArgs {
8686
std::vector<jobject> globalRefs_;
8787
};
8888

89+
jsi::Value createJSRuntimeError(
90+
jsi::Runtime& runtime,
91+
const std::string& message) {
92+
return runtime.global()
93+
.getPropertyAsFunction(runtime, "Error")
94+
.call(runtime, message);
95+
}
96+
97+
jsi::Value createRejectionError(jsi::Runtime& rt, const folly::dynamic& args) {
98+
react_native_assert(
99+
args.size() == 1 && "promise reject should has only one argument");
100+
101+
auto value = jsi::valueFromDynamic(rt, args[0]);
102+
react_native_assert(value.isObject() && "promise reject should return a map");
103+
104+
const jsi::Object& valueAsObject = value.asObject(rt);
105+
106+
auto messageProperty = valueAsObject.getProperty(rt, "message");
107+
auto jsError =
108+
createJSRuntimeError(rt, messageProperty.asString(rt).utf8(rt));
109+
110+
auto jsErrorAsObject = jsError.asObject(rt);
111+
auto propertyNames = valueAsObject.getPropertyNames(rt);
112+
for (size_t i = 0; i < propertyNames.size(rt); ++i) {
113+
auto propertyName = jsi::PropNameID::forString(
114+
rt, propertyNames.getValueAtIndex(rt, i).asString(rt));
115+
jsErrorAsObject.setProperty(
116+
rt, propertyName, valueAsObject.getProperty(rt, propertyName));
117+
}
118+
return jsError;
119+
}
120+
89121
auto createJavaCallback(
90122
jsi::Runtime& rt,
91123
jsi::Function&& function,
@@ -98,7 +130,6 @@ auto createJavaCallback(
98130
LOG(FATAL) << "Callback arg cannot be called more than once";
99131
return;
100132
}
101-
102133
callback->call([args = std::move(args)](
103134
jsi::Runtime& rt, jsi::Function& jsFunction) {
104135
std::vector<jsi::Value> jsArgs;
@@ -112,6 +143,26 @@ auto createJavaCallback(
112143
});
113144
}
114145

146+
auto createJavaRejectCallback(
147+
jsi::Runtime& rt,
148+
jsi::Function&& function,
149+
std::shared_ptr<CallInvoker> jsInvoker) {
150+
std::optional<AsyncCallback<>> callback(
151+
{rt, std::move(function), std::move(jsInvoker)});
152+
return JCxxCallbackImpl::newObjectCxxArgs(
153+
[callback = std::move(callback)](folly::dynamic args) mutable {
154+
if (!callback) {
155+
LOG(FATAL) << "Callback arg cannot be called more than once";
156+
return;
157+
}
158+
callback->call([args = std::move(args)](
159+
jsi::Runtime& rt, jsi::Function& jsFunction) {
160+
jsFunction.call(rt, createRejectionError(rt, args));
161+
});
162+
callback = std::nullopt;
163+
});
164+
}
165+
115166
struct JPromiseImpl : public jni::JavaClass<JPromiseImpl> {
116167
constexpr static auto kJavaDescriptor =
117168
"Lcom/facebook/react/bridge/PromiseImpl;";
@@ -407,14 +458,6 @@ jsi::Value convertFromJMapToValue(JNIEnv* env, jsi::Runtime& rt, jobject arg) {
407458
return jsi::valueFromDynamic(rt, result->cthis()->consume());
408459
}
409460

410-
jsi::Value createJSRuntimeError(
411-
jsi::Runtime& runtime,
412-
const std::string& message) {
413-
return runtime.global()
414-
.getPropertyAsFunction(runtime, "Error")
415-
.call(runtime, message);
416-
}
417-
418461
/**
419462
* Creates JSError with current JS runtime stack and Throwable stack trace.
420463
*/
@@ -855,7 +898,7 @@ jsi::Value JavaTurboModule::invokeJavaMethod(
855898
runtime,
856899
args[0].getObject(runtime).getFunction(runtime),
857900
jsInvoker_);
858-
auto reject = createJavaCallback(
901+
auto reject = createJavaRejectCallback(
859902
runtime,
860903
args[1].getObject(runtime).getFunction(runtime),
861904
jsInvoker_);

0 commit comments

Comments
 (0)