2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2018-09-12 01:27:47 +03:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-03-23 23:28:42 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-23 23:28:42 +03:00
|
|
|
*/
|
2015-02-20 07:10:52 +03:00
|
|
|
|
|
|
|
#import "RCTExceptionsManager.h"
|
|
|
|
|
2019-08-16 05:57:09 +03:00
|
|
|
#import <FBReactNativeSpec/FBReactNativeSpec.h>
|
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTDefines.h>
|
|
|
|
#import <React/RCTLog.h>
|
|
|
|
#import <React/RCTRedBox.h>
|
2019-10-30 22:20:29 +03:00
|
|
|
#import <React/RCTReloadCommand.h>
|
2019-08-16 05:57:09 +03:00
|
|
|
#import <React/RCTRootView.h>
|
|
|
|
|
|
|
|
#import "CoreModulesPlugins.h"
|
|
|
|
|
|
|
|
@interface RCTExceptionsManager() <NativeExceptionsManagerSpec>
|
|
|
|
|
|
|
|
@end
|
2015-02-20 07:10:52 +03:00
|
|
|
|
|
|
|
@implementation RCTExceptionsManager
|
2015-03-11 23:53:30 +03:00
|
|
|
|
2015-08-19 15:27:43 +03:00
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
2015-04-08 15:42:43 +03:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-03-11 23:53:30 +03:00
|
|
|
- (instancetype)initWithDelegate:(id<RCTExceptionsManagerDelegate>)delegate
|
|
|
|
{
|
2015-11-25 14:09:00 +03:00
|
|
|
if ((self = [self init])) {
|
2015-03-11 23:53:30 +03:00
|
|
|
_delegate = delegate;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-11-01 02:22:38 +03:00
|
|
|
- (void)reportSoft: (NSString *)message stack:(NSArray<NSDictionary *> *)stack exceptionId:(double)exceptionId suppressRedBox: (BOOL) suppressRedBox {
|
|
|
|
if (!suppressRedBox) {
|
|
|
|
[_bridge.redBox showErrorMessage:message withStack:stack errorCookie:((int)exceptionId)];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_delegate) {
|
|
|
|
[_delegate handleSoftJSExceptionWithMessage:message stack:stack exceptionId:[NSNumber numberWithDouble:exceptionId]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reportFatal: (NSString *)message stack:(NSArray<NSDictionary *> *)stack exceptionId:(double)exceptionId suppressRedBox: (BOOL) suppressRedBox {
|
|
|
|
if (!suppressRedBox) {
|
|
|
|
[_bridge.redBox showErrorMessage:message withStack:stack errorCookie:((int)exceptionId)];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_delegate) {
|
|
|
|
[_delegate handleFatalJSExceptionWithMessage:message stack:stack exceptionId:[NSNumber numberWithDouble:exceptionId]];
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSUInteger reloadRetries = 0;
|
|
|
|
if (!RCT_DEBUG && reloadRetries < _maxReloadAttempts) {
|
|
|
|
reloadRetries++;
|
|
|
|
RCTTriggerReloadCommandListeners(@"JS Crash Reload");
|
|
|
|
} else if (!RCT_DEV || !suppressRedBox) {
|
|
|
|
NSString *description = [@"Unhandled JS Exception: " stringByAppendingString:message];
|
|
|
|
NSDictionary *errorInfo = @{ NSLocalizedDescriptionKey: description, RCTJSStackTraceKey: stack };
|
|
|
|
RCTFatal([NSError errorWithDomain:RCTErrorDomain code:0 userInfo:errorInfo]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-13 20:56:09 +03:00
|
|
|
RCT_EXPORT_METHOD(reportSoftException:(NSString *)message
|
2015-12-10 21:09:04 +03:00
|
|
|
stack:(NSArray<NSDictionary *> *)stack
|
2019-08-16 05:57:09 +03:00
|
|
|
exceptionId:(double)exceptionId)
|
2015-05-13 20:56:09 +03:00
|
|
|
{
|
2019-11-01 02:22:38 +03:00
|
|
|
[self reportSoft:message stack:stack exceptionId:exceptionId suppressRedBox:NO];
|
2015-05-13 20:56:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(reportFatalException:(NSString *)message
|
2015-12-10 21:09:04 +03:00
|
|
|
stack:(NSArray<NSDictionary *> *)stack
|
2019-08-16 05:57:09 +03:00
|
|
|
exceptionId:(double) exceptionId)
|
2015-02-20 07:10:52 +03:00
|
|
|
{
|
2019-11-01 02:22:38 +03:00
|
|
|
[self reportFatal:message stack:stack exceptionId:exceptionId suppressRedBox:NO];
|
2015-02-20 07:10:52 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 18:52:48 +03:00
|
|
|
RCT_EXPORT_METHOD(updateExceptionMessage:(NSString *)message
|
2015-12-10 21:09:04 +03:00
|
|
|
stack:(NSArray<NSDictionary *> *)stack
|
2019-08-16 05:57:09 +03:00
|
|
|
exceptionId:(double)exceptionId)
|
2015-02-20 07:10:52 +03:00
|
|
|
{
|
2019-09-06 20:10:23 +03:00
|
|
|
[_bridge.redBox updateErrorMessage:message withStack:stack errorCookie:((int)exceptionId)];
|
2015-11-05 23:19:56 +03:00
|
|
|
|
|
|
|
if (_delegate && [_delegate respondsToSelector:@selector(updateJSExceptionWithMessage:stack:exceptionId:)]) {
|
2019-08-16 05:57:09 +03:00
|
|
|
[_delegate updateJSExceptionWithMessage:message stack:stack exceptionId:[NSNumber numberWithDouble:exceptionId]];
|
2015-11-05 23:19:56 +03:00
|
|
|
}
|
2015-02-20 07:10:52 +03:00
|
|
|
}
|
|
|
|
|
2015-05-13 20:56:09 +03:00
|
|
|
// Deprecated. Use reportFatalException directly instead.
|
|
|
|
RCT_EXPORT_METHOD(reportUnhandledException:(NSString *)message
|
2015-12-10 21:09:04 +03:00
|
|
|
stack:(NSArray<NSDictionary *> *)stack)
|
2015-05-13 20:56:09 +03:00
|
|
|
{
|
2019-08-16 05:57:09 +03:00
|
|
|
[self reportFatalException:message stack:stack exceptionId:-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(dismissRedbox)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(reportException:(JS::NativeExceptionsManager::ExceptionData &)data)
|
|
|
|
{
|
2019-09-06 20:10:23 +03:00
|
|
|
NSString *message = data.message();
|
|
|
|
double exceptionId = data.id_();
|
2019-11-01 02:22:38 +03:00
|
|
|
id<NSObject> extraData = data.extraData();
|
2019-09-06 20:10:23 +03:00
|
|
|
|
|
|
|
// Reserialize data.stack() into an array of untyped dictionaries.
|
|
|
|
// TODO: (moti) T53588496 Replace `(NSArray<NSDictionary *> *)stack` in
|
|
|
|
// reportFatalException etc with a typed interface.
|
|
|
|
NSMutableArray<NSDictionary *> *stackArray = [NSMutableArray<NSDictionary *> new];
|
|
|
|
for (auto frame: data.stack()) {
|
|
|
|
NSMutableDictionary * frameDict = [NSMutableDictionary new];
|
|
|
|
if (frame.column().hasValue()) {
|
|
|
|
frameDict[@"column"] = @(frame.column().value());
|
|
|
|
}
|
|
|
|
frameDict[@"file"] = frame.file();
|
|
|
|
if (frame.lineNumber().hasValue()) {
|
|
|
|
frameDict[@"lineNumber"] = @(frame.lineNumber().value());
|
|
|
|
}
|
|
|
|
frameDict[@"methodName"] = frame.methodName();
|
|
|
|
if (frame.collapse().hasValue()) {
|
|
|
|
frameDict[@"collapse"] = @(frame.collapse().value());
|
|
|
|
}
|
|
|
|
[stackArray addObject:frameDict];
|
|
|
|
}
|
2019-11-01 02:22:38 +03:00
|
|
|
NSDictionary *dict = (NSDictionary *)extraData;
|
|
|
|
BOOL suppressRedBox = [[dict objectForKey:@"suppressRedBox"] boolValue];
|
2019-08-16 05:57:09 +03:00
|
|
|
|
2019-09-06 20:10:23 +03:00
|
|
|
if (data.isFatal()) {
|
2019-11-01 02:22:38 +03:00
|
|
|
[self reportFatal:message stack:stackArray exceptionId:exceptionId suppressRedBox:suppressRedBox];
|
2019-09-06 20:10:23 +03:00
|
|
|
} else {
|
2019-11-01 02:22:38 +03:00
|
|
|
[self reportSoft:message stack:stackArray exceptionId:exceptionId suppressRedBox:suppressRedBox];
|
2019-09-06 20:10:23 +03:00
|
|
|
}
|
2019-08-16 05:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:
|
2019-09-20 20:48:50 +03:00
|
|
|
(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
|
2019-08-16 05:57:09 +03:00
|
|
|
{
|
|
|
|
return std::make_shared<facebook::react::NativeExceptionsManagerSpecJSI>(self, jsInvoker);
|
2015-05-13 20:56:09 +03:00
|
|
|
}
|
2015-11-05 23:19:56 +03:00
|
|
|
|
2015-02-20 07:10:52 +03:00
|
|
|
@end
|
2019-08-16 05:57:09 +03:00
|
|
|
|
|
|
|
Class RCTExceptionsManagerCls(void)
|
|
|
|
{
|
|
|
|
return RCTExceptionsManager.class;
|
|
|
|
}
|