Skip to content

Commit 8e731ea

Browse files
javachefacebook-github-bot
authored andcommitted
RCTTurboModule nits (#54692)
Summary: Lazily create error strings and use jsi::JSError for more user-friendly error messages Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D87868701
1 parent dba18bb commit 8e731ea

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
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: 22 additions & 19 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,14 +268,12 @@ 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) mutable {
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.");
273+
throw jsi::JSError(
274+
rt,
275+
moduleName + "." + methodName + "(): Promise must pass constructor function two args. Passed " +
276+
std::to_string(count) + " args.");
279277
}
280278
if (!invokeCopy) {
281279
return jsi::Value::undefined();
@@ -306,12 +304,16 @@ id convertJSIValueToObjCObject(
306304
}
307305

308306
if (alreadyResolved) {
309-
RCTLogError(@"%s: Tried to resolve a promise more than once.", moduleMethod.c_str());
307+
RCTLogError(
308+
@"%s.%s(): Tried to resolve a promise more than once.", moduleName.c_str(), methodName.c_str());
310309
return;
311310
}
312311

313312
if (alreadyRejected) {
314-
RCTLogError(@"%s: Tried to resolve a promise after it's already been rejected.", moduleMethod.c_str());
313+
RCTLogError(
314+
@"%s.%s(): Tried to resolve a promise after it's already been rejected.",
315+
moduleName.c_str(),
316+
methodName.c_str());
315317
return;
316318
}
317319

@@ -339,16 +341,18 @@ id convertJSIValueToObjCObject(
339341

340342
if (alreadyResolved) {
341343
RCTLogError(
342-
@"%s: Tried to reject a promise after it's already been resolved. Message: %s",
343-
moduleMethod.c_str(),
344+
@"%s.%s() Tried to reject a promise after it's already been resolved. Message: %s",
345+
moduleName.c_str(),
346+
methodName.c_str(),
344347
message.UTF8String);
345348
return;
346349
}
347350

348351
if (alreadyRejected) {
349352
RCTLogError(
350-
@"%s: Tried to reject a promise more than once. Message: %s",
351-
moduleMethod.c_str(),
353+
@"%s.%s() Tried to reject a promise more than once. Message: %s",
354+
moduleName.c_str(),
355+
methodName.c_str(),
352356
message.UTF8String);
353357
return;
354358
}
@@ -527,9 +531,9 @@ TraceSection s(
527531
break;
528532
}
529533
case FunctionKind:
530-
throw std::runtime_error("convertReturnIdToJSIValue: FunctionKind is not supported yet.");
534+
throw jsi::JSError(runtime, "convertReturnIdToJSIValue: FunctionKind is not supported yet.");
531535
case PromiseKind:
532-
throw std::runtime_error("convertReturnIdToJSIValue: PromiseKind wasn't handled properly.");
536+
throw jsi::JSError(runtime, "convertReturnIdToJSIValue: PromiseKind wasn't handled properly.");
533537
}
534538

535539
return returnValue;
@@ -711,14 +715,13 @@ TraceSection s(
711715
NSMutableArray *retainedObjectsForInvocation)
712716
{
713717
const char *moduleName = name_.c_str();
714-
const NSObject<RCTBridgeModule> *module = instance_;
715-
716718
if (isSync) {
717719
TurboModulePerfLogger::syncMethodCallArgConversionStart(moduleName, methodName);
718720
} else {
719721
TurboModulePerfLogger::asyncMethodCallArgConversionStart(moduleName, methodName);
720722
}
721723

724+
const NSObject<RCTBridgeModule> *module = instance_;
722725
NSMethodSignature *methodSignature = [module methodSignatureForSelector:selector];
723726
if (count > methodSignature.numberOfArguments - 2) {
724727
throw jsi::JSError(
@@ -751,7 +754,7 @@ TraceSection s(
751754
return true;
752755
}
753756

754-
return !(returnType == VoidKind || returnType == PromiseKind);
757+
return returnType != VoidKind && returnType != PromiseKind;
755758
}
756759

757760
ObjCTurboModule::ObjCTurboModule(const InitParams &params)

0 commit comments

Comments
 (0)