2015-03-23 20:28:42 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-03-23 20:28:42 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00: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 20:28:42 +00:00
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTRedBox.h"
|
|
|
|
|
2015-04-11 22:08:00 +00:00
|
|
|
#import "RCTBridge.h"
|
2015-08-04 23:18:44 +00:00
|
|
|
#import "RCTConvert.h"
|
2015-04-30 16:50:22 +00:00
|
|
|
#import "RCTDefines.h"
|
2016-07-18 18:27:55 +00:00
|
|
|
#import "RCTErrorInfo.h"
|
2017-12-14 18:38:34 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
2016-07-11 13:05:51 +00:00
|
|
|
#import "RCTJSStackFrame.h"
|
2017-12-14 18:38:34 +00:00
|
|
|
#import "RCTRedBoxExtraDataViewController.h"
|
2016-11-23 15:47:52 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-30 16:50:22 +00:00
|
|
|
#if RCT_DEBUG
|
|
|
|
|
2016-04-28 18:38:18 +00:00
|
|
|
@class RCTRedBoxWindow;
|
|
|
|
|
|
|
|
@protocol RCTRedBoxWindowActionDelegate <NSObject>
|
|
|
|
|
2016-07-11 13:05:51 +00:00
|
|
|
- (void)redBoxWindow:(RCTRedBoxWindow *)redBoxWindow openStackFrameInEditor:(RCTJSStackFrame *)stackFrame;
|
2016-04-28 18:38:18 +00:00
|
|
|
- (void)reloadFromRedBoxWindow:(RCTRedBoxWindow *)redBoxWindow;
|
2017-12-14 18:38:34 +00:00
|
|
|
- (void)loadExtraDataViewController;
|
2016-04-28 18:38:18 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@interface RCTRedBoxWindow : UIWindow <UITableViewDelegate, UITableViewDataSource>
|
2016-04-28 18:38:18 +00:00
|
|
|
@property (nonatomic, weak) id<RCTRedBoxWindowActionDelegate> actionDelegate;
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTRedBoxWindow
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
UITableView *_stackTraceTableView;
|
|
|
|
NSString *_lastErrorMessage;
|
|
|
|
NSArray<RCTJSStackFrame *> *_lastStackTrace;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
if ((self = [super initWithFrame:frame])) {
|
2017-11-21 02:12:42 +00:00
|
|
|
#if TARGET_OS_TV
|
2017-12-14 18:38:34 +00:00
|
|
|
self.windowLevel = UIWindowLevelAlert + 1000;
|
2017-11-21 02:12:42 +00:00
|
|
|
#else
|
2017-12-14 18:38:34 +00:00
|
|
|
self.windowLevel = UIWindowLevelStatusBar - 1;
|
2017-11-21 02:12:42 +00:00
|
|
|
#endif
|
2017-12-14 18:38:34 +00:00
|
|
|
self.backgroundColor = [UIColor colorWithRed:0.8 green:0 blue:0 alpha:1];
|
|
|
|
self.hidden = YES;
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
UIViewController *rootController = [UIViewController new];
|
|
|
|
self.rootViewController = rootController;
|
|
|
|
UIView *rootView = rootController.view;
|
|
|
|
rootView.backgroundColor = [UIColor clearColor];
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
const CGFloat buttonHeight = 60;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
CGRect detailsFrame = rootView.bounds;
|
|
|
|
detailsFrame.size.height -= buttonHeight;
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
_stackTraceTableView = [[UITableView alloc] initWithFrame:detailsFrame style:UITableViewStylePlain];
|
|
|
|
_stackTraceTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
_stackTraceTableView.delegate = self;
|
|
|
|
_stackTraceTableView.dataSource = self;
|
|
|
|
_stackTraceTableView.backgroundColor = [UIColor clearColor];
|
2016-09-27 13:19:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
2017-12-14 18:38:34 +00:00
|
|
|
_stackTraceTableView.separatorColor = [UIColor colorWithWhite:1 alpha:0.3];
|
|
|
|
_stackTraceTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
|
|
|
|
#endif
|
|
|
|
_stackTraceTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
|
|
|
|
[rootView addSubview:_stackTraceTableView];
|
|
|
|
|
|
|
|
#if TARGET_OS_SIMULATOR
|
|
|
|
NSString *reloadText = @"Reload JS (\u2318R)";
|
|
|
|
NSString *dismissText = @"Dismiss (ESC)";
|
|
|
|
NSString *copyText = @"Copy (\u2325\u2318C)";
|
|
|
|
NSString *extraText = @"Extra Info (\u2318E)";
|
|
|
|
#else
|
|
|
|
NSString *reloadText = @"Reload JS";
|
|
|
|
NSString *dismissText = @"Dismiss";
|
|
|
|
NSString *copyText = @"Copy";
|
|
|
|
NSString *extraText = @"Extra Info";
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2017-12-14 18:38:34 +00:00
|
|
|
|
|
|
|
UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
|
dismissButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
|
|
|
|
dismissButton.accessibilityIdentifier = @"redbox-dismiss";
|
|
|
|
dismissButton.titleLabel.font = [UIFont systemFontOfSize:13];
|
|
|
|
[dismissButton setTitle:dismissText forState:UIControlStateNormal];
|
|
|
|
[dismissButton setTitleColor:[UIColor colorWithWhite:1 alpha:0.5] forState:UIControlStateNormal];
|
|
|
|
[dismissButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
|
|
|
|
[dismissButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
|
|
|
|
UIButton *reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
|
reloadButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
|
|
|
|
reloadButton.accessibilityIdentifier = @"redbox-reload";
|
|
|
|
reloadButton.titleLabel.font = [UIFont systemFontOfSize:13];
|
|
|
|
|
|
|
|
[reloadButton setTitle:reloadText forState:UIControlStateNormal];
|
|
|
|
[reloadButton setTitleColor:[UIColor colorWithWhite:1 alpha:0.5] forState:UIControlStateNormal];
|
|
|
|
[reloadButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
|
|
|
|
[reloadButton addTarget:self action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
|
|
|
|
UIButton *copyButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
|
copyButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
|
|
|
|
copyButton.accessibilityIdentifier = @"redbox-copy";
|
|
|
|
copyButton.titleLabel.font = [UIFont systemFontOfSize:13];
|
|
|
|
[copyButton setTitle:copyText forState:UIControlStateNormal];
|
|
|
|
[copyButton setTitleColor:[UIColor colorWithWhite:1 alpha:0.5] forState:UIControlStateNormal];
|
|
|
|
[copyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
|
|
|
|
[copyButton addTarget:self action:@selector(copyStack) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
|
|
|
|
UIButton *extraButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
|
|
extraButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
|
|
|
|
extraButton.accessibilityIdentifier = @"redbox-extra";
|
|
|
|
extraButton.titleLabel.font = [UIFont systemFontOfSize:13];
|
|
|
|
[extraButton setTitle:extraText forState:UIControlStateNormal];
|
|
|
|
[extraButton setTitleColor:[UIColor colorWithWhite:1 alpha:0.5] forState:UIControlStateNormal];
|
|
|
|
[extraButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
|
|
|
|
[extraButton addTarget:self action:@selector(showExtraDataViewController) forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
|
|
|
|
CGFloat buttonWidth = self.bounds.size.width / 4;
|
|
|
|
dismissButton.frame = CGRectMake(0, self.bounds.size.height - buttonHeight, buttonWidth, buttonHeight);
|
|
|
|
reloadButton.frame = CGRectMake(buttonWidth, self.bounds.size.height - buttonHeight, buttonWidth, buttonHeight);
|
|
|
|
copyButton.frame = CGRectMake(buttonWidth * 2, self.bounds.size.height - buttonHeight, buttonWidth, buttonHeight);
|
|
|
|
extraButton.frame = CGRectMake(buttonWidth * 3, self.bounds.size.height - buttonHeight, buttonWidth, buttonHeight);
|
|
|
|
|
|
|
|
[rootView addSubview:dismissButton];
|
|
|
|
[rootView addSubview:reloadButton];
|
|
|
|
[rootView addSubview:copyButton];
|
|
|
|
[rootView addSubview:extraButton];
|
|
|
|
}
|
|
|
|
return self;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-05-07 10:53:35 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
_stackTraceTableView.dataSource = nil;
|
|
|
|
_stackTraceTableView.delegate = nil;
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
2015-05-07 10:53:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 13:05:51 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withStack:(NSArray<RCTJSStackFrame *> *)stack isUpdate:(BOOL)isUpdate
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
// Show if this is a new message, or if we're updating the previous message
|
|
|
|
if ((self.hidden && !isUpdate) || (!self.hidden && isUpdate && [_lastErrorMessage isEqualToString:message])) {
|
|
|
|
_lastStackTrace = stack;
|
|
|
|
// message is displayed using UILabel, which is unable to render text of
|
|
|
|
// unlimited length, so we truncate it
|
|
|
|
_lastErrorMessage = [message substringToIndex:MIN((NSUInteger)10000, message.length)];
|
2015-02-25 00:33:39 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
[_stackTraceTableView reloadData];
|
2015-02-25 00:33:39 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
if (self.hidden) {
|
|
|
|
[_stackTraceTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
|
|
|
|
atScrollPosition:UITableViewScrollPositionTop
|
|
|
|
animated:NO];
|
|
|
|
}
|
2015-03-18 23:01:46 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
[self makeKeyAndVisible];
|
|
|
|
[self becomeFirstResponder];
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dismiss
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
self.hidden = YES;
|
|
|
|
[self resignFirstResponder];
|
|
|
|
[RCTSharedApplication().delegate.window makeKeyWindow];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reload
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
[_actionDelegate reloadFromRedBoxWindow:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)showExtraDataViewController
|
|
|
|
{
|
|
|
|
[_actionDelegate loadExtraDataViewController];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 19:27:10 +00:00
|
|
|
- (void)copyStack
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
NSMutableString *fullStackTrace;
|
2016-05-27 18:38:28 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
if (_lastErrorMessage != nil) {
|
|
|
|
fullStackTrace = [_lastErrorMessage mutableCopy];
|
|
|
|
[fullStackTrace appendString:@"\n\n"];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fullStackTrace = [NSMutableString string];
|
|
|
|
}
|
2016-05-27 18:38:28 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
for (RCTJSStackFrame *stackFrame in _lastStackTrace) {
|
|
|
|
[fullStackTrace appendString:[NSString stringWithFormat:@"%@\n", stackFrame.methodName]];
|
|
|
|
if (stackFrame.file) {
|
|
|
|
[fullStackTrace appendFormat:@" %@\n", [self formatFrameSource:stackFrame]];
|
|
|
|
}
|
2016-05-17 19:27:10 +00:00
|
|
|
}
|
2016-09-27 13:19:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
2017-12-14 18:38:34 +00:00
|
|
|
UIPasteboard *pb = [UIPasteboard generalPasteboard];
|
|
|
|
[pb setString:fullStackTrace];
|
2016-09-27 13:19:45 +00:00
|
|
|
#endif
|
2016-05-17 19:27:10 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 13:05:51 +00:00
|
|
|
- (NSString *)formatFrameSource:(RCTJSStackFrame *)stackFrame
|
2016-05-27 18:38:28 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
NSString *fileName = RCTNilIfNull(stackFrame.file) ? [stackFrame.file lastPathComponent] : @"<unknown file>";
|
|
|
|
NSString *lineInfo = [NSString stringWithFormat:@"%@:%lld",
|
|
|
|
fileName,
|
|
|
|
(long long)stackFrame.lineNumber];
|
2016-05-27 18:38:28 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
if (stackFrame.column != 0) {
|
|
|
|
lineInfo = [lineInfo stringByAppendingFormat:@":%lld", (long long)stackFrame.column];
|
|
|
|
}
|
|
|
|
return lineInfo;
|
2016-05-27 18:38:28 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
#pragma mark - TableView
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (NSInteger)numberOfSectionsInTableView:(__unused UITableView *)tableView
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
return 2;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (NSInteger)tableView:(__unused UITableView *)tableView numberOfRowsInSection:(NSInteger)section
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
return section == 0 ? 1 : _lastStackTrace.count;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
if (indexPath.section == 0) {
|
|
|
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"msg-cell"];
|
|
|
|
return [self reuseCell:cell forErrorMessage:_lastErrorMessage];
|
|
|
|
}
|
|
|
|
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
|
|
|
|
NSUInteger index = indexPath.row;
|
|
|
|
RCTJSStackFrame *stackFrame = _lastStackTrace[index];
|
|
|
|
return [self reuseCell:cell forStackFrame:stackFrame];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UITableViewCell *)reuseCell:(UITableViewCell *)cell forErrorMessage:(NSString *)message
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
if (!cell) {
|
|
|
|
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"msg-cell"];
|
|
|
|
cell.textLabel.accessibilityIdentifier = @"redbox-error";
|
|
|
|
cell.textLabel.textColor = [UIColor whiteColor];
|
|
|
|
cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
|
|
|
|
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
|
|
|
|
cell.textLabel.numberOfLines = 0;
|
|
|
|
cell.detailTextLabel.textColor = [UIColor whiteColor];
|
|
|
|
cell.backgroundColor = [UIColor clearColor];
|
|
|
|
cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
cell.textLabel.text = message;
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
return cell;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 13:05:51 +00:00
|
|
|
- (UITableViewCell *)reuseCell:(UITableViewCell *)cell forStackFrame:(RCTJSStackFrame *)stackFrame
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
if (!cell) {
|
|
|
|
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
|
|
|
|
cell.textLabel.textColor = [UIColor colorWithWhite:1 alpha:0.9];
|
|
|
|
cell.textLabel.font = [UIFont fontWithName:@"Menlo-Regular" size:14];
|
|
|
|
cell.textLabel.lineBreakMode = NSLineBreakByCharWrapping;
|
|
|
|
cell.textLabel.numberOfLines = 2;
|
|
|
|
cell.detailTextLabel.textColor = [UIColor colorWithWhite:1 alpha:0.7];
|
|
|
|
cell.detailTextLabel.font = [UIFont fontWithName:@"Menlo-Regular" size:11];
|
|
|
|
cell.detailTextLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
|
|
|
|
cell.backgroundColor = [UIColor clearColor];
|
|
|
|
cell.selectedBackgroundView = [UIView new];
|
|
|
|
cell.selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.textLabel.text = stackFrame.methodName ?: @"(unnamed method)";
|
|
|
|
if (stackFrame.file) {
|
|
|
|
cell.detailTextLabel.text = [self formatFrameSource:stackFrame];
|
|
|
|
} else {
|
|
|
|
cell.detailTextLabel.text = @"";
|
|
|
|
}
|
|
|
|
return cell;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
if (indexPath.section == 0) {
|
|
|
|
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
|
|
|
|
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16],
|
|
|
|
NSParagraphStyleAttributeName: paragraphStyle};
|
|
|
|
CGRect boundingRect = [_lastErrorMessage boundingRectWithSize:CGSizeMake(tableView.frame.size.width - 30, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
|
|
|
|
return ceil(boundingRect.size.height) + 40;
|
|
|
|
} else {
|
|
|
|
return 50;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
if (indexPath.section == 1) {
|
|
|
|
NSUInteger row = indexPath.row;
|
|
|
|
RCTJSStackFrame *stackFrame = _lastStackTrace[row];
|
|
|
|
[_actionDelegate redBoxWindow:self openStackFrameInEditor:stackFrame];
|
|
|
|
}
|
|
|
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Key commands
|
|
|
|
|
2015-11-03 22:45:46 +00:00
|
|
|
- (NSArray<UIKeyCommand *> *)keyCommands
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
// NOTE: We could use RCTKeyCommands for this, but since
|
|
|
|
// we control this window, we can use the standard, non-hacky
|
|
|
|
// mechanism instead
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
return @[
|
|
|
|
// Dismiss red box
|
|
|
|
[UIKeyCommand keyCommandWithInput:UIKeyInputEscape
|
|
|
|
modifierFlags:0
|
|
|
|
action:@selector(dismiss)],
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
// Reload
|
|
|
|
[UIKeyCommand keyCommandWithInput:@"r"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:@selector(reload)],
|
2016-05-17 19:27:10 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
// Copy = Cmd-Option C since Cmd-C in the simulator copies the pasteboard from
|
|
|
|
// the simulator to the desktop pasteboard.
|
|
|
|
[UIKeyCommand keyCommandWithInput:@"c"
|
|
|
|
modifierFlags:UIKeyModifierCommand | UIKeyModifierAlternate
|
|
|
|
action:@selector(copyStack)],
|
2016-05-17 19:27:10 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
// Extra data
|
|
|
|
[UIKeyCommand keyCommandWithInput:@"e"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:@selector(showExtraDataViewController)]
|
|
|
|
];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)canBecomeFirstResponder
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
return YES;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
@interface RCTRedBox () <RCTInvalidating, RCTRedBoxWindowActionDelegate, RCTRedBoxExtraDataActionDelegate>
|
2015-09-15 18:11:28 +00:00
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTRedBox
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
RCTRedBoxWindow *_window;
|
|
|
|
NSMutableArray<id<RCTErrorCustomizer>> *_errorCustomizers;
|
|
|
|
RCTRedBoxExtraDataViewController *_extraDataViewController;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 18:38:18 +00:00
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
2015-08-19 12:27:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
2015-06-09 21:51:25 +00:00
|
|
|
|
2016-07-18 18:27:55 +00:00
|
|
|
- (void)registerErrorCustomizer:(id<RCTErrorCustomizer>)errorCustomizer
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
if (!self->_errorCustomizers) {
|
|
|
|
self->_errorCustomizers = [NSMutableArray array];
|
|
|
|
}
|
|
|
|
if (![self->_errorCustomizers containsObject:errorCustomizer]) {
|
|
|
|
[self->_errorCustomizers addObject:errorCustomizer];
|
|
|
|
}
|
|
|
|
});
|
2016-07-18 18:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING: Should only be called from the main thread/dispatch queue.
|
|
|
|
- (RCTErrorInfo *)_customizeError:(RCTErrorInfo *)error
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
RCTAssertMainQueue();
|
|
|
|
if (!self->_errorCustomizers) {
|
|
|
|
return error;
|
2016-07-18 18:27:55 +00:00
|
|
|
}
|
2017-12-14 18:38:34 +00:00
|
|
|
for (id<RCTErrorCustomizer> customizer in self->_errorCustomizers) {
|
|
|
|
RCTErrorInfo *newInfo = [customizer customizeErrorInfo:error];
|
|
|
|
if (newInfo) {
|
|
|
|
error = newInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return error;
|
2016-07-18 18:27:55 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 23:11:42 +00:00
|
|
|
- (void)showError:(NSError *)error
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
[self showErrorMessage:error.localizedDescription
|
|
|
|
withDetails:error.localizedFailureReason
|
|
|
|
stack:error.userInfo[RCTJSStackTraceKey]];
|
2015-07-14 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
[self showErrorMessage:message withParsedStack:nil isUpdate:NO];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)showErrorMessage:(NSString *)message withDetails:(NSString *)details
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
[self showErrorMessage:message withDetails:details stack:nil];
|
2017-04-11 12:54:15 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 20:17:07 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withDetails:(NSString *)details stack:(NSArray<RCTJSStackFrame *> *)stack {
|
2017-12-14 18:38:34 +00:00
|
|
|
NSString *combinedMessage = message;
|
|
|
|
if (details) {
|
|
|
|
combinedMessage = [NSString stringWithFormat:@"%@\n\n%@", message, details];
|
|
|
|
}
|
|
|
|
[self showErrorMessage:combinedMessage withParsedStack:stack isUpdate:NO];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-01 02:38:53 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withRawStack:(NSString *)rawStack
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
NSArray<RCTJSStackFrame *> *stack = [RCTJSStackFrame stackFramesWithLines:rawStack];
|
|
|
|
[self showErrorMessage:message withParsedStack:stack isUpdate:NO];
|
2016-07-01 02:38:53 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 20:17:07 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withStack:(NSArray<NSDictionary *> *)stack
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
[self showErrorMessage:message withParsedStack:[RCTJSStackFrame stackFramesWithDictionaries:stack] isUpdate:NO];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 20:17:07 +00:00
|
|
|
- (void)updateErrorMessage:(NSString *)message withStack:(NSArray<NSDictionary *> *)stack
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
[self showErrorMessage:message withParsedStack:[RCTJSStackFrame stackFramesWithDictionaries:stack] isUpdate:YES];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-05 20:17:07 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withParsedStack:(NSArray<RCTJSStackFrame *> *)stack
|
2016-07-11 13:05:51 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
[self showErrorMessage:message withParsedStack:stack isUpdate:NO];
|
2017-10-05 20:17:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)updateErrorMessage:(NSString *)message withParsedStack:(NSArray<RCTJSStackFrame *> *)stack
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
[self showErrorMessage:message withParsedStack:stack isUpdate:YES];
|
2017-10-05 20:17:07 +00:00
|
|
|
}
|
2016-07-11 13:05:51 +00:00
|
|
|
|
2017-10-05 20:17:07 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withParsedStack:(NSArray<RCTJSStackFrame *> *)stack isUpdate:(BOOL)isUpdate
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
if (self->_extraDataViewController == nil) {
|
|
|
|
self->_extraDataViewController = [RCTRedBoxExtraDataViewController new];
|
|
|
|
self->_extraDataViewController.actionDelegate = self;
|
|
|
|
}
|
|
|
|
[self->_bridge.eventDispatcher sendDeviceEventWithName:@"collectRedBoxExtraData" body:nil];
|
|
|
|
|
|
|
|
if (!self->_window) {
|
|
|
|
self->_window = [[RCTRedBoxWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
|
|
|
|
self->_window.actionDelegate = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCTErrorInfo *errorInfo = [[RCTErrorInfo alloc] initWithErrorMessage:message
|
|
|
|
stack:stack];
|
|
|
|
errorInfo = [self _customizeError:errorInfo];
|
|
|
|
[self->_window showErrorMessage:errorInfo.errorMessage
|
|
|
|
withStack:errorInfo.stack
|
|
|
|
isUpdate:isUpdate];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadExtraDataViewController {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
// Make sure the CMD+E shortcut doesn't call this twice
|
|
|
|
if (self->_extraDataViewController != nil && ![self->_window.rootViewController presentedViewController]) {
|
|
|
|
[self->_window.rootViewController presentViewController:self->_extraDataViewController animated:YES completion:nil];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(setExtraData:(NSDictionary *)extraData forIdentifier:(NSString *)identifier) {
|
|
|
|
[_extraDataViewController addExtraData:extraData forIdentifier:identifier];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 22:55:02 +00:00
|
|
|
RCT_EXPORT_METHOD(dismiss)
|
2015-03-14 01:32:38 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[self->_window dismiss];
|
|
|
|
});
|
2015-09-15 18:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
[self dismiss];
|
2015-03-14 01:32:38 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 12:51:56 +00:00
|
|
|
- (void)redBoxWindow:(__unused RCTRedBoxWindow *)redBoxWindow openStackFrameInEditor:(RCTJSStackFrame *)stackFrame
|
2016-04-28 18:38:18 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
NSURL *const bundleURL = _overrideBundleURL ?: _bridge.bundleURL;
|
|
|
|
if (![bundleURL.scheme hasPrefix:@"http"]) {
|
|
|
|
RCTLogWarn(@"Cannot open stack frame in editor because you're not connected to the packager.");
|
|
|
|
return;
|
|
|
|
}
|
2016-04-28 18:38:18 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
NSData *stackFrameJSON = [RCTJSONStringify([stackFrame toDictionary], NULL) dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSString *postLength = [NSString stringWithFormat:@"%tu", stackFrameJSON.length];
|
|
|
|
NSMutableURLRequest *request = [NSMutableURLRequest new];
|
|
|
|
request.URL = [NSURL URLWithString:@"/open-stack-frame" relativeToURL:bundleURL];
|
|
|
|
request.HTTPMethod = @"POST";
|
|
|
|
request.HTTPBody = stackFrameJSON;
|
|
|
|
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
|
|
|
|
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
|
2016-04-28 18:38:18 +00:00
|
|
|
|
2017-12-14 18:38:34 +00:00
|
|
|
[[[NSURLSession sharedSession] dataTaskWithRequest:request] resume];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reload
|
|
|
|
{
|
|
|
|
// Window is not used and can be nil
|
|
|
|
[self reloadFromRedBoxWindow:nil];
|
2016-04-28 18:38:18 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 00:27:55 +00:00
|
|
|
- (void)reloadFromRedBoxWindow:(__unused RCTRedBoxWindow *)redBoxWindow
|
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
if (_overrideReloadAction) {
|
|
|
|
_overrideReloadAction();
|
|
|
|
} else {
|
|
|
|
[_bridge reload];
|
|
|
|
}
|
|
|
|
[self dismiss];
|
2016-04-28 18:38:18 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 12:27:43 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTBridge (RCTRedBox)
|
|
|
|
|
|
|
|
- (RCTRedBox *)redBox
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2017-12-14 18:38:34 +00:00
|
|
|
return [self moduleForClass:[RCTRedBox class]];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2015-04-21 16:48:29 +00:00
|
|
|
|
2015-04-30 16:50:22 +00:00
|
|
|
#else // Disabled
|
|
|
|
|
|
|
|
@implementation RCTRedBox
|
|
|
|
|
2015-08-25 11:27:09 +00:00
|
|
|
+ (NSString *)moduleName { return nil; }
|
2016-07-18 18:27:55 +00:00
|
|
|
- (void)registerErrorCustomizer:(id<RCTErrorCustomizer>)errorCustomizer {}
|
2015-07-14 23:11:42 +00:00
|
|
|
- (void)showError:(NSError *)message {}
|
2015-04-30 16:50:22 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message {}
|
|
|
|
- (void)showErrorMessage:(NSString *)message withDetails:(NSString *)details {}
|
2016-07-01 10:12:57 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withRawStack:(NSString *)rawStack {}
|
2015-11-03 22:45:46 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withStack:(NSArray<NSDictionary *> *)stack {}
|
|
|
|
- (void)updateErrorMessage:(NSString *)message withStack:(NSArray<NSDictionary *> *)stack {}
|
2017-10-05 20:17:07 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withParsedStack:(NSArray<RCTJSStackFrame *> *)stack {}
|
|
|
|
- (void)updateErrorMessage:(NSString *)message withParsedStack:(NSArray<RCTJSStackFrame *> *)stack {}
|
2016-07-12 12:51:56 +00:00
|
|
|
- (void)showErrorMessage:(NSString *)message withStack:(NSArray<NSDictionary *> *)stack isUpdate:(BOOL)isUpdate {}
|
2015-04-30 16:50:22 +00:00
|
|
|
- (void)dismiss {}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-08-19 12:27:43 +00:00
|
|
|
@implementation RCTBridge (RCTRedBox)
|
|
|
|
|
|
|
|
- (RCTRedBox *)redBox { return nil; }
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-04-21 16:48:29 +00:00
|
|
|
#endif
|