Skip to content

Commit

Permalink
Migrate JS error handler to MapBuffer
Browse files Browse the repository at this point in the history
Summary:
Changelog:
[iOS][Changed] Replace Folly with MapBuffer for passing js error data

Reviewed By: sammy-SC

Differential Revision: D38460203

fbshipit-source-id: 98f6243e31da42cc601727545b331392300cee20
  • Loading branch information
luluwu2032 authored and facebook-github-bot committed Aug 25, 2022
1 parent 7909b91 commit e6ef083
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
5 changes: 4 additions & 1 deletion React/CoreModules/BUCK
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@fbsource//tools/build_defs/apple:flag_defs.bzl", "get_objc_arc_preprocessor_flags", "get_preprocessor_flags_for_build_mode")
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "rn_apple_library", "rn_extra_build_flags")
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "react_native_xplat_target", "rn_apple_library", "rn_extra_build_flags")
load(
"@fbsource//xplat/configurations/buck/apple/plugins/sad_xplat_hosted_configurations:react_module_registration.bzl",
"react_module_plugin_providers",
Expand Down Expand Up @@ -129,6 +129,9 @@ rn_apple_library(
],
reexport_all_header_dependencies = True,
visibility = ["PUBLIC"],
deps = [
react_native_xplat_target("react/renderer/mapbuffer:mapbufferApple"),
],
exported_deps = [
"//xplat/js/react-native-github:FBReactNativeSpecApple",
"//xplat/js/react-native-github:RCTLinkingApple",
Expand Down
3 changes: 2 additions & 1 deletion React/CoreModules/RCTExceptionsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <react/renderer/mapbuffer/MapBuffer.h>

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -38,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)reportFatalException:(nullable NSString *)message
stack:(nullable NSArray<NSDictionary *> *)stack
exceptionId:(double)exceptionId;
- (void)reportJsException:(NSString *)errorMap;
- (void)reportJsException:(const facebook::react::MapBuffer &)errorMap;

@property (nonatomic, weak) id<RCTExceptionsManagerDelegate> delegate;

Expand Down
39 changes: 27 additions & 12 deletions React/CoreModules/RCTExceptionsManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ @implementation RCTExceptionsManager

@synthesize moduleRegistry = _moduleRegistry;

const int FILE_KEY_OF_JS_ERROR = 0;
const int METHOD_NAME_KEY_OF_JS_ERROR = 1;
const int LINE_NUMBER_KEY_OF_JS_ERROR = 2;
const int COLUMN_KEY_OF_JS_ERROR = 3;
const int FRAMES_KEY_OF_JS_ERROR = 4;
const int MESSAGE_KEY_OF_JS_ERROR = 5;
const int ID_KEY_OF_JS_ERROR = 6;
const int IS_FATAL_KEY_OF_JS_ERROR = 7;

RCT_EXPORT_MODULE()

- (instancetype)initWithDelegate:(id<RCTExceptionsManagerDelegate>)delegate
Expand Down Expand Up @@ -150,19 +159,25 @@ - (void)reportFatal:(NSString *)message
}
}

- (void)reportJsException:(NSString *)errorStr
- (void)reportJsException:(const facebook::react::MapBuffer &)errorMap
{
NSData *jsonData = [errorStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONWritingPrettyPrinted
error:&jsonError];

NSString *message = [dict objectForKey:@"message"];
double exceptionId = [[dict objectForKey:@"id"] doubleValue];
NSArray *stack = [dict objectForKey:@"stack"];
BOOL isFatal = [[dict objectForKey:@"isFatal"] boolValue];

NSString *message = [NSString stringWithCString:errorMap.getString(MESSAGE_KEY_OF_JS_ERROR).c_str()
encoding:[NSString defaultCStringEncoding]];
int exceptionId = errorMap.getInt(ID_KEY_OF_JS_ERROR);
BOOL isFatal = errorMap.getBool(IS_FATAL_KEY_OF_JS_ERROR);
std::vector<facebook::react::MapBuffer> frames = errorMap.getMapBufferList(FRAMES_KEY_OF_JS_ERROR);
NSMutableArray *stack = [[NSMutableArray alloc] init];
for (facebook::react::MapBuffer const &mapBuffer : frames) {
NSDictionary *frame = @{
@"file" : [NSString stringWithCString:mapBuffer.getString(FILE_KEY_OF_JS_ERROR).c_str()
encoding:[NSString defaultCStringEncoding]],
@"methodName" : [NSString stringWithCString:mapBuffer.getString(METHOD_NAME_KEY_OF_JS_ERROR).c_str()
encoding:[NSString defaultCStringEncoding]],
@"lineNumber" : [NSNumber numberWithInt:mapBuffer.getInt(LINE_NUMBER_KEY_OF_JS_ERROR)],
@"column" : [NSNumber numberWithInt:mapBuffer.getInt(COLUMN_KEY_OF_JS_ERROR)],
};
[stack addObject:frame];
}
if (isFatal) {
[self reportFatalException:message stack:stack exceptionId:exceptionId];
} else {
Expand Down

0 comments on commit e6ef083

Please sign in to comment.