1// Copyright 2015-present 650 Industries. All rights reserved. 2 3#import "EXAppViewController.h" 4#import "EXErrorRecoveryManager.h" 5#import "EXKernel.h" 6#import "EXKernelAppRecord.h" 7#import "EXReactAppManager.h" 8#import "EXReactAppExceptionHandler.h" 9#import "EXUtil.h" 10 11#import <React/RCTBridge.h> 12#import <React/RCTRedBox.h> 13 14RCTFatalHandler handleFatalReactError = ^(NSError *error) { 15 [EXUtil performSynchronouslyOnMainThread:^{ 16 EXKernelAppRecord *record = [[EXKernel sharedInstance].serviceRegistry.errorRecoveryManager appRecordForError:error]; 17 if (!record) { 18 // show the error on Home or on the main standalone app if we can't figure out who this error belongs to 19 if ([EXKernel sharedInstance].appRegistry.homeAppRecord) { 20 record = [EXKernel sharedInstance].appRegistry.homeAppRecord; 21 } else if ([EXKernel sharedInstance].appRegistry.standaloneAppRecord) { 22 record = [EXKernel sharedInstance].appRegistry.standaloneAppRecord; 23 } 24 } 25 if (record) { 26 [record.viewController maybeShowError:error]; 27 } 28 }]; 29}; 30 31NS_ASSUME_NONNULL_BEGIN 32 33@interface EXReactAppExceptionHandler () 34 35@property (nonatomic, weak) EXKernelAppRecord *appRecord; 36 37@end 38 39@implementation EXReactAppExceptionHandler 40 41- (instancetype)initWithAppRecord:(EXKernelAppRecord *)appRecord 42{ 43 if (self = [super init]) { 44 _appRecord = appRecord; 45 } 46 return self; 47} 48 49RCT_NOT_IMPLEMENTED(- (instancetype)init) 50 51- (void)handleSoftJSExceptionWithMessage:(nullable NSString *)message 52 stack:(nullable NSArray<NSDictionary<NSString *, id> *> *)stack 53 exceptionId:(NSNumber *)exceptionId 54{ 55 // In RN 0.8 this was used to invoke the native red box errors (via `showErrorMessage`). 56 // The invocation has since been moved into the method that invokes this delegate method. 57 // https://github.com/facebook/react-native/commit/31b5b0ac010d44afe3e742e85c75a9ef9e72a9e0#diff-35e5d8a9e7e9ea80b2ccfd41b905b703 58} 59 60- (void)handleFatalJSExceptionWithMessage:(nullable NSString *)message 61 stack:(nullable NSArray<NSDictionary<NSString *, id> *> *)stack 62 exceptionId:(NSNumber *)exceptionId 63{ 64 NSString *description = [@"Unhandled JS Exception: " stringByAppendingString:message]; 65 NSDictionary *errorInfo = @{ NSLocalizedDescriptionKey: description, RCTJSStackTraceKey: stack }; 66 NSError *error = [NSError errorWithDomain:RCTErrorDomain code:0 userInfo:errorInfo]; 67 68 [[EXKernel sharedInstance].serviceRegistry.errorRecoveryManager setError:error forScopeKey:_appRecord.scopeKey]; 69 70 if ([self _isProdHome]) { 71 RCTFatal(error); 72 } 73} 74 75- (void)updateJSExceptionWithMessage:(nullable NSString *)message 76 stack:(nullable NSArray *)stack 77 exceptionId:(NSNumber *)exceptionId 78{ 79 [[self _bridgeForRecord].redBox updateErrorMessage:message withStack:stack]; 80} 81 82#pragma mark - internal 83 84- (RCTBridge *)_bridgeForRecord 85{ 86 return _appRecord.appManager.reactBridge; 87} 88 89- (BOOL)_isProdHome 90{ 91 if (RCT_DEBUG) { 92 return NO; 93 } 94 return (_appRecord && _appRecord == [EXKernel sharedInstance].appRegistry.homeAppRecord); 95} 96 97@end 98 99NS_ASSUME_NONNULL_END 100