From 68bb3a7e7159c3682996d6ff601fcdd98a1aa2cc Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Tue, 9 Jun 2015 14:51:25 -0700 Subject: [PATCH] [ReactNative] orange box Summary: @public There have been multiple instances of confusion about whether a redbox means the developer did something they shouldn't but things will keep working, or if something went horribly wrong and the app will crash in prod. This diff introduces an orange background color to the redbox for `console.error` and `RCTLogError` to indicate that something bad happened, but that the app will continue working. Test Plan: see orange error for geo permissions: {F22541375} --- React/Base/RCTLog.m | 3 +++ React/Base/RCTRedBox.h | 2 ++ React/Base/RCTRedBox.m | 17 +++++++++++++++-- React/Modules/RCTExceptionsManager.m | 4 +++- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/React/Base/RCTLog.m b/React/Base/RCTLog.m index d99bb4fe1..9469d3015 100644 --- a/React/Base/RCTLog.m +++ b/React/Base/RCTLog.m @@ -171,6 +171,9 @@ void _RCTLogFormat( // Log to red box if (level >= RCTLOG_REDBOX_LEVEL) { + if (level < RCTLOG_FATAL_LEVEL) { + [[RCTRedBox sharedInstance] setNextBackgroundColor:[UIColor colorWithRed:0.9 green:0.4 blue:0.2 alpha:1]]; + } [[RCTRedBox sharedInstance] showErrorMessage:message]; } diff --git a/React/Base/RCTRedBox.h b/React/Base/RCTRedBox.h index 9a3a9b49a..7a1f51f36 100644 --- a/React/Base/RCTRedBox.h +++ b/React/Base/RCTRedBox.h @@ -18,6 +18,8 @@ - (void)showErrorMessage:(NSString *)message withStack:(NSArray *)stack; - (void)updateErrorMessage:(NSString *)message withStack:(NSArray *)stack; +- (void)setNextBackgroundColor:(UIColor *)color; + - (NSString *)currentErrorMessage; - (void)dismiss; diff --git a/React/Base/RCTRedBox.m b/React/Base/RCTRedBox.m index 6330454ab..b293a162d 100644 --- a/React/Base/RCTRedBox.m +++ b/React/Base/RCTRedBox.m @@ -29,14 +29,15 @@ NSArray *_lastStackTrace; UITableViewCell *_cachedMessageCell; + UIColor *_redColor; } - (id)initWithFrame:(CGRect)frame { if ((self = [super initWithFrame:frame])) { - + _redColor = [UIColor colorWithRed:0.8 green:0 blue:0 alpha:1]; self.windowLevel = UIWindowLevelStatusBar + 5; - self.backgroundColor = [UIColor colorWithRed:0.8 green:0 blue:0 alpha:1]; + self.backgroundColor = _redColor; self.hidden = YES; UIViewController *rootController = [[UIViewController alloc] init]; @@ -132,6 +133,7 @@ - (void)dismiss { self.hidden = YES; + self.backgroundColor = _redColor; [self resignFirstResponder]; [[[[UIApplication sharedApplication] delegate] window] makeKeyWindow]; } @@ -261,6 +263,7 @@ @implementation RCTRedBox { RCTRedBoxWindow *_window; + UIColor *_nextBackgroundColor; } + (instancetype)sharedInstance @@ -273,6 +276,11 @@ return _sharedInstance; } +- (void)setNextBackgroundColor:(UIColor *)color +{ + _nextBackgroundColor = color; +} + - (void)showErrorMessage:(NSString *)message { [self showErrorMessage:message withStack:nil showIfHidden:YES]; @@ -304,6 +312,10 @@ _window = [[RCTRedBoxWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; } [_window showErrorMessage:message withStack:stack showIfHidden:shouldShow]; + if (_nextBackgroundColor) { + _window.backgroundColor = _nextBackgroundColor; + _nextBackgroundColor = nil; + } }); } @@ -334,6 +346,7 @@ - (void)updateErrorMessage:(NSString *)message withStack:(NSArray *)stack {} - (void)showErrorMessage:(NSString *)message withStack:(NSArray *)stack showIfHidden:(BOOL)shouldShow {} - (NSString *)currentErrorMessage { return nil; } +- (void)setNextBackgroundColor:(UIColor *)color {} - (void)dismiss {} @end diff --git a/React/Modules/RCTExceptionsManager.m b/React/Modules/RCTExceptionsManager.m index 64a5c85f0..4a9815f31 100644 --- a/React/Modules/RCTExceptionsManager.m +++ b/React/Modules/RCTExceptionsManager.m @@ -44,7 +44,9 @@ RCT_EXPORT_METHOD(reportSoftException:(NSString *)message [_delegate handleSoftJSExceptionWithMessage:message stack:stack]; return; } - // JS already logs the error via console. + RCTRedBox *box = [RCTRedBox sharedInstance]; + [box setNextBackgroundColor:[UIColor colorWithRed:0.9 green:0.4 blue:0.2 alpha:1]]; + [box showErrorMessage:message withStack:stack]; } RCT_EXPORT_METHOD(reportFatalException:(NSString *)message