push error handling c++ parsing down (#37893)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/37893

Changelog: [Internal]

i just noticed we can push more C++ code down into RCTInstance, so let's do that! will make everything easier to test as well

Reviewed By: cipolleschi, RSNara

Differential Revision: D46672345

fbshipit-source-id: fcb878698ebc9a19714ba6ff94a5760efc830958
This commit is contained in:
Phillip Pan
2023-06-14 17:14:02 -07:00
committed by Facebook GitHub Bot
parent 7d69cd5640
commit 1cac95d076
3 changed files with 32 additions and 23 deletions
@@ -234,28 +234,13 @@ using namespace facebook::react;
#pragma mark - RCTInstanceDelegate
- (void)instance:(RCTInstance *)instance didReceiveErrorMap:(facebook::react::MapBuffer)errorMap
- (void)instance:(RCTInstance *)instance
didReceiveJSErrorStack:(NSArray<NSDictionary<NSString *, id> *> *)stack
message:(NSString *)message
exceptionId:(NSUInteger)exceptionId
isFatal:(BOOL)isFatal
{
NSString *message = [NSString stringWithCString:errorMap.getString(JSErrorHandlerKey::kErrorMessage).c_str()
encoding:[NSString defaultCStringEncoding]];
std::vector<facebook::react::MapBuffer> frames = errorMap.getMapBufferList(JSErrorHandlerKey::kAllStackFrames);
NSMutableArray<NSDictionary<NSString *, id> *> *stack = [NSMutableArray new];
for (facebook::react::MapBuffer const &mapBuffer : frames) {
NSDictionary *frame = @{
@"file" : [NSString stringWithCString:mapBuffer.getString(JSErrorHandlerKey::kFrameFileName).c_str()
encoding:[NSString defaultCStringEncoding]],
@"methodName" : [NSString stringWithCString:mapBuffer.getString(JSErrorHandlerKey::kFrameMethodName).c_str()
encoding:[NSString defaultCStringEncoding]],
@"lineNumber" : [NSNumber numberWithInt:mapBuffer.getInt(JSErrorHandlerKey::kFrameLineNumber)],
@"column" : [NSNumber numberWithInt:mapBuffer.getInt(JSErrorHandlerKey::kFrameColumnNumber)],
};
[stack addObject:frame];
}
[_hostDelegate host:self
didReceiveJSErrorStack:stack
message:message
exceptionId:errorMap.getInt(JSErrorHandlerKey::kExceptionId)
isFatal:errorMap.getBool(JSErrorHandlerKey::kIsFatal)];
[_hostDelegate host:self didReceiveJSErrorStack:stack message:message exceptionId:exceptionId isFatal:isFatal];
}
- (void)instance:(RCTInstance *)instance didInitializeRuntime:(facebook::jsi::Runtime &)runtime
@@ -38,7 +38,12 @@ FB_RUNTIME_PROTOCOL
@protocol RCTInstanceDelegate <RCTContextContainerHandling>
- (void)instance:(RCTInstance *)instance didReceiveErrorMap:(facebook::react::MapBuffer)errorMap;
- (void)instance:(RCTInstance *)instance
didReceiveJSErrorStack:(NSArray<NSDictionary<NSString *, id> *> *)stack
message:(NSString *)message
exceptionId:(NSUInteger)exceptionId
isFatal:(BOOL)isFatal;
- (void)instance:(RCTInstance *)instance didInitializeRuntime:(facebook::jsi::Runtime &)runtime;
@end
@@ -413,7 +413,26 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags)
- (void)_handleJSErrorMap:(facebook::react::MapBuffer)errorMap
{
[_delegate instance:self didReceiveErrorMap:std::move(errorMap)];
NSString *message = [NSString stringWithCString:errorMap.getString(JSErrorHandlerKey::kErrorMessage).c_str()
encoding:[NSString defaultCStringEncoding]];
std::vector<facebook::react::MapBuffer> frames = errorMap.getMapBufferList(JSErrorHandlerKey::kAllStackFrames);
NSMutableArray<NSDictionary<NSString *, id> *> *stack = [NSMutableArray new];
for (facebook::react::MapBuffer const &mapBuffer : frames) {
NSDictionary *frame = @{
@"file" : [NSString stringWithCString:mapBuffer.getString(JSErrorHandlerKey::kFrameFileName).c_str()
encoding:[NSString defaultCStringEncoding]],
@"methodName" : [NSString stringWithCString:mapBuffer.getString(JSErrorHandlerKey::kFrameMethodName).c_str()
encoding:[NSString defaultCStringEncoding]],
@"lineNumber" : [NSNumber numberWithInt:mapBuffer.getInt(JSErrorHandlerKey::kFrameLineNumber)],
@"column" : [NSNumber numberWithInt:mapBuffer.getInt(JSErrorHandlerKey::kFrameColumnNumber)],
};
[stack addObject:frame];
}
[_delegate instance:self
didReceiveJSErrorStack:stack
message:message
exceptionId:errorMap.getInt(JSErrorHandlerKey::kExceptionId)
isFatal:errorMap.getBool(JSErrorHandlerKey::kIsFatal)];
}
@end