From 31b4a92aa95454c6a5e08b637d0dab1fdd1459f1 Mon Sep 17 00:00:00 2001 From: Lulu Wu Date: Fri, 26 Aug 2022 03:52:57 -0700 Subject: [PATCH] Revert D38460203: Migrate JS error handler to MapBuffer Differential Revision: D38460203 (https://github.com/facebook/react-native/commit/e6ef0836c132d6b798e2ff1fc1e1a66c7e238e0b) Original commit changeset: 98f6243e31da Original Phabricator Diff: D38460203 (https://github.com/facebook/react-native/commit/e6ef0836c132d6b798e2ff1fc1e1a66c7e238e0b) fbshipit-source-id: 540a48c807cea7f2898f261b82010da729f3421e --- React/CoreModules/BUCK | 5 +-- React/CoreModules/RCTExceptionsManager.h | 3 +- React/CoreModules/RCTExceptionsManager.mm | 39 +++++++---------------- 3 files changed, 14 insertions(+), 33 deletions(-) diff --git a/React/CoreModules/BUCK b/React/CoreModules/BUCK index 750745304e9090..c5f95ab7009ceb 100644 --- a/React/CoreModules/BUCK +++ b/React/CoreModules/BUCK @@ -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", "react_native_xplat_target", "rn_apple_library", "rn_extra_build_flags") +load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "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", @@ -129,9 +129,6 @@ 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", diff --git a/React/CoreModules/RCTExceptionsManager.h b/React/CoreModules/RCTExceptionsManager.h index 69a1f5c870effd..76d36423d722aa 100644 --- a/React/CoreModules/RCTExceptionsManager.h +++ b/React/CoreModules/RCTExceptionsManager.h @@ -7,7 +7,6 @@ #import #import -#import NS_ASSUME_NONNULL_BEGIN @@ -39,7 +38,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)reportFatalException:(nullable NSString *)message stack:(nullable NSArray *)stack exceptionId:(double)exceptionId; -- (void)reportJsException:(const facebook::react::MapBuffer &)errorMap; +- (void)reportJsException:(NSString *)errorMap; @property (nonatomic, weak) id delegate; diff --git a/React/CoreModules/RCTExceptionsManager.mm b/React/CoreModules/RCTExceptionsManager.mm index 985ba16fcc7927..9d6aafc044be4f 100644 --- a/React/CoreModules/RCTExceptionsManager.mm +++ b/React/CoreModules/RCTExceptionsManager.mm @@ -25,15 +25,6 @@ @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)delegate @@ -159,25 +150,19 @@ - (void)reportFatal:(NSString *)message } } -- (void)reportJsException:(const facebook::react::MapBuffer &)errorMap +- (void)reportJsException:(NSString *)errorStr { - 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 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]; - } + 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]; + if (isFatal) { [self reportFatalException:message stack:stack exceptionId:exceptionId]; } else {