@@ -57,7 +57,7 @@ static int32_t getUniqueId()
57
57
58
58
static jsi::String convertNSStringToJSIString (jsi::Runtime &runtime, NSString *value)
59
59
{
60
- return jsi::String::createFromUtf8 (runtime, [value UTF8String ] ?: " " );
60
+ return jsi::String::createFromUtf8 (runtime, [value UTF8String ] ? [value UTF8String ] : " " );
61
61
}
62
62
63
63
static jsi::Object convertNSDictionaryToJSIObject (jsi::Runtime &runtime, NSDictionary *value)
@@ -213,7 +213,11 @@ id convertJSIValueToObjCObject(
213
213
/* *
214
214
* Creates JSError with current JS runtime and NSException stack trace.
215
215
*/
216
- static jsi::JSError convertNSExceptionToJSError (jsi::Runtime &runtime, NSException *exception )
216
+ static jsi::JSError convertNSExceptionToJSError (
217
+ jsi::Runtime &runtime,
218
+ NSException *exception ,
219
+ const std::string &moduleName,
220
+ const std::string &methodName)
217
221
{
218
222
std::string reason = [exception .reason UTF8String ];
219
223
@@ -224,7 +228,8 @@ id convertJSIValueToObjCObject(
224
228
cause.setProperty (
225
229
runtime, " stackReturnAddresses" , convertNSArrayToJSIArray (runtime, exception .callStackReturnAddresses ));
226
230
227
- jsi::Value error = createJSRuntimeError (runtime, " Exception in HostFunction: " + reason);
231
+ std::string message = moduleName + " ." + methodName + " raised an exception: " + reason;
232
+ jsi::Value error = createJSRuntimeError (runtime, message);
228
233
error.asObject (runtime).setProperty (runtime, " cause" , std::move (cause));
229
234
return {runtime, std::move (error)};
230
235
}
@@ -356,28 +361,34 @@ id convertJSIValueToObjCObject(
356
361
}
357
362
358
363
if (isSync) {
359
- TurboModulePerfLogger::syncMethodCallExecutionStart (moduleName, methodNameStr. c_str () );
364
+ TurboModulePerfLogger::syncMethodCallExecutionStart (moduleName, methodName );
360
365
} else {
361
- TurboModulePerfLogger::asyncMethodCallExecutionStart (moduleName, methodNameStr. c_str () , asyncCallCounter);
366
+ TurboModulePerfLogger::asyncMethodCallExecutionStart (moduleName, methodName , asyncCallCounter);
362
367
}
363
368
364
369
@try {
365
370
[inv invokeWithTarget: strongModule];
366
371
} @catch (NSException *exception ) {
367
- throw convertNSExceptionToJSError (runtime, exception );
372
+ if (isSync) {
373
+ // We can only convert NSException to JSError in sync method calls.
374
+ // See https://github.com/reactwg/react-native-new-architecture/discussions/276#discussioncomment-12567155
375
+ throw convertNSExceptionToJSError (runtime, exception , std::string{moduleName}, methodNameStr);
376
+ } else {
377
+ @throw exception ;
378
+ }
368
379
} @finally {
369
380
[retainedObjectsForInvocation removeAllObjects ];
370
381
}
371
382
372
383
if (!isSync) {
373
- TurboModulePerfLogger::asyncMethodCallExecutionEnd (moduleName, methodNameStr. c_str () , asyncCallCounter);
384
+ TurboModulePerfLogger::asyncMethodCallExecutionEnd (moduleName, methodName , asyncCallCounter);
374
385
return ;
375
386
}
376
387
377
388
void *rawResult;
378
389
[inv getReturnValue: &rawResult];
379
390
result = (__bridge id )rawResult;
380
- TurboModulePerfLogger::syncMethodCallExecutionEnd (moduleName, methodNameStr. c_str () );
391
+ TurboModulePerfLogger::syncMethodCallExecutionEnd (moduleName, methodName );
381
392
};
382
393
383
394
if (isSync) {
@@ -419,23 +430,23 @@ TraceSection s(
419
430
}
420
431
421
432
if (shouldVoidMethodsExecuteSync_) {
422
- TurboModulePerfLogger::syncMethodCallExecutionStart (moduleName, methodNameStr. c_str () );
433
+ TurboModulePerfLogger::syncMethodCallExecutionStart (moduleName, methodName );
423
434
} else {
424
- TurboModulePerfLogger::asyncMethodCallExecutionStart (moduleName, methodNameStr. c_str () , asyncCallCounter);
435
+ TurboModulePerfLogger::asyncMethodCallExecutionStart (moduleName, methodName , asyncCallCounter);
425
436
}
426
437
427
438
@try {
428
439
[inv invokeWithTarget: strongModule];
429
440
} @catch (NSException *exception ) {
430
- throw convertNSExceptionToJSError (runtime, exception );
441
+ throw convertNSExceptionToJSError (runtime, exception , std::string{moduleName}, methodNameStr );
431
442
} @finally {
432
443
[retainedObjectsForInvocation removeAllObjects ];
433
444
}
434
445
435
446
if (shouldVoidMethodsExecuteSync_) {
436
- TurboModulePerfLogger::syncMethodCallExecutionEnd (moduleName, methodNameStr. c_str () );
447
+ TurboModulePerfLogger::syncMethodCallExecutionEnd (moduleName, methodName );
437
448
} else {
438
- TurboModulePerfLogger::asyncMethodCallExecutionEnd (moduleName, methodNameStr. c_str () , asyncCallCounter);
449
+ TurboModulePerfLogger::asyncMethodCallExecutionEnd (moduleName, methodName , asyncCallCounter);
439
450
}
440
451
441
452
return ;
0 commit comments