Skip to content

Commit d49e07a

Browse files
javachemeta-codesync[bot]
authored andcommitted
RCTTurboModule nits (facebook#54692)
Summary: Pull Request resolved: facebook#54692 Lazily create error strings and use jsi::JSError for more user-friendly error messages Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D87868701 fbshipit-source-id: a38be93511520329a3c703b1b27dbb456da30be2
1 parent 0b09e6f commit d49e07a

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,6 @@ T RCTConvertTo(SEL selector, id json)
600600
TurboModuleMethodValueKind returnType,
601601
id result)
602602
{
603-
std::string methodJsSignature = name_ + "." + methodNameCStr + "()";
604-
std::string errorPrefix =
605-
methodJsSignature + ": Error while converting return Objective C value to JavaScript type. ";
606-
607603
if (returnType == VoidKind) {
608604
return jsi::Value::undefined();
609605
}
@@ -617,6 +613,7 @@ T RCTConvertTo(SEL selector, id json)
617613
return returnValue;
618614
}
619615

616+
std::string methodJsSignature = name_ + "." + methodNameCStr + "()";
620617
throw jsi::JSError(runtime, methodJsSignature + "Objective C type was unsupported.");
621618
}
622619

packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ id convertJSIValueToObjCObject(
199199
return convertJSIObjectToNSDictionary(runtime, o, jsInvoker, useNSNull);
200200
}
201201

202-
throw std::runtime_error("Unsupported jsi::Value kind");
202+
throw jsi::JSError(runtime, "Unsupported jsi::Value kind");
203203
}
204204

205205
static jsi::Value createJSRuntimeError(jsi::Runtime &runtime, const std::string &message)
@@ -268,24 +268,17 @@ id convertJSIValueToObjCObject(
268268
jsi::PropNameID::forAscii(runtime, "fn"),
269269
2,
270270
[invokeCopy, jsInvoker = jsInvoker_, moduleName = name_, methodName](
271-
jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *args, size_t count) {
272-
// FIXME: do not allocate this upfront
273-
std::string moduleMethod = moduleName + "." + methodName + "()";
274-
271+
jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) {
275272
if (count != 2) {
276-
throw std::invalid_argument(
277-
moduleMethod + ": Promise must pass constructor function two args. Passed " + std::to_string(count) +
278-
" args.");
279-
}
280-
if (!invokeCopy) {
281-
return jsi::Value::undefined();
273+
throw jsi::JSError(
274+
rt,
275+
moduleName + "." + methodName + "(): Promise must pass constructor function two args. Passed " +
276+
std::to_string(count) + " args.");
282277
}
283278

284279
__block BOOL resolveWasCalled = NO;
285-
__block std::optional<AsyncCallback<>> resolve(
286-
{rt, args[0].getObject(rt).getFunction(rt), std::move(jsInvoker)});
287-
__block std::optional<AsyncCallback<>> reject(
288-
{rt, args[1].getObject(rt).getFunction(rt), std::move(jsInvoker)});
280+
__block std::optional<AsyncCallback<>> resolve({rt, args[0].getObject(rt).getFunction(rt), jsInvoker});
281+
__block std::optional<AsyncCallback<>> reject({rt, args[1].getObject(rt).getFunction(rt), jsInvoker});
289282
__block std::shared_ptr<std::mutex> mutex = std::make_shared<std::mutex>();
290283

291284
RCTPromiseResolveBlock resolveBlock = ^(id result) {
@@ -306,12 +299,14 @@ id convertJSIValueToObjCObject(
306299
}
307300

308301
if (alreadyResolved) {
309-
RCTLogError(@"%s: Tried to resolve a promise more than once.", moduleMethod.c_str());
302+
RCTLogError(
303+
@"%s.%s(): Tried to resolve a promise more than once.", moduleName.c_str(), methodName.c_str());
310304
return;
311-
}
312-
313-
if (alreadyRejected) {
314-
RCTLogError(@"%s: Tried to resolve a promise after it's already been rejected.", moduleMethod.c_str());
305+
} else if (alreadyRejected) {
306+
RCTLogError(
307+
@"%s.%s(): Tried to resolve a promise after it's already been rejected.",
308+
moduleName.c_str(),
309+
methodName.c_str());
315310
return;
316311
}
317312

@@ -339,16 +334,16 @@ id convertJSIValueToObjCObject(
339334

340335
if (alreadyResolved) {
341336
RCTLogError(
342-
@"%s: Tried to reject a promise after it's already been resolved. Message: %s",
343-
moduleMethod.c_str(),
337+
@"%s.%s() Tried to reject a promise after it's already been resolved. Message: %s",
338+
moduleName.c_str(),
339+
methodName.c_str(),
344340
message.UTF8String);
345341
return;
346-
}
347-
348-
if (alreadyRejected) {
342+
} else if (alreadyRejected) {
349343
RCTLogError(
350-
@"%s: Tried to reject a promise more than once. Message: %s",
351-
moduleMethod.c_str(),
344+
@"%s.%s() Tried to reject a promise more than once. Message: %s",
345+
moduleName.c_str(),
346+
methodName.c_str(),
352347
message.UTF8String);
353348
return;
354349
}
@@ -527,9 +522,9 @@ TraceSection s(
527522
break;
528523
}
529524
case FunctionKind:
530-
throw std::runtime_error("convertReturnIdToJSIValue: FunctionKind is not supported yet.");
525+
throw jsi::JSError(runtime, "convertReturnIdToJSIValue: FunctionKind is not supported yet.");
531526
case PromiseKind:
532-
throw std::runtime_error("convertReturnIdToJSIValue: PromiseKind wasn't handled properly.");
527+
throw jsi::JSError(runtime, "convertReturnIdToJSIValue: PromiseKind wasn't handled properly.");
533528
}
534529

535530
return returnValue;
@@ -711,14 +706,13 @@ TraceSection s(
711706
NSMutableArray *retainedObjectsForInvocation)
712707
{
713708
const char *moduleName = name_.c_str();
714-
const NSObject<RCTBridgeModule> *module = instance_;
715-
716709
if (isSync) {
717710
TurboModulePerfLogger::syncMethodCallArgConversionStart(moduleName, methodName);
718711
} else {
719712
TurboModulePerfLogger::asyncMethodCallArgConversionStart(moduleName, methodName);
720713
}
721714

715+
const NSObject<RCTBridgeModule> *module = instance_;
722716
NSMethodSignature *methodSignature = [module methodSignatureForSelector:selector];
723717
if (count > methodSignature.numberOfArguments - 2) {
724718
throw jsi::JSError(
@@ -751,7 +745,7 @@ TraceSection s(
751745
return true;
752746
}
753747

754-
return !(returnType == VoidKind || returnType == PromiseKind);
748+
return returnType != VoidKind && returnType != PromiseKind;
755749
}
756750

757751
ObjCTurboModule::ObjCTurboModule(const InitParams &params)

0 commit comments

Comments
 (0)