Skip to content

Commit 49d26eb

Browse files
thymikeefacebook-github-bot
authored andcommitted
cleanup RedBox message and stack output (#24662)
Summary: Cleanup RedBox messages and stack traces. This PR consists of 2 changes (I'm good with splitting them up if you'd like): - [general] filter out some of the internal callsites from the symbolicated stack (I thought about using monospace font for title with code frame, but it looks weird) - [ios][android] strip ANSI characters (coming from colored Babel code frame) from the error message I think it's ok to strip it inside native handlers so we can still have a colorful code frame in the terminal output. **JS Code frame:** |before|after| |--|--| |<img width="400" alt="Screenshot 2019-04-30 at 12 32 05" src="https://user-images.githubusercontent.com/5106466/56956590-ef678d80-6b44-11e9-9019-6801f050ab0d.png">|<img width="400" alt="Screenshot 2019-04-30 at 12 52 43" src="https://user-images.githubusercontent.com/5106466/56957302-f42d4100-6b46-11e9-869b-ea9c7ce5b90f.png">| |before|after| |--|--| |![image](https://user-images.githubusercontent.com/5106466/56959472-c8618980-6b4d-11e9-84be-6261d8375f4a.png)|![image](https://user-images.githubusercontent.com/5106466/56959463-bc75c780-6b4d-11e9-9d8b-25ffe46c87cf.png)| **Filtered stack traces:** |before|after| |--|--| |<img width="50%" alt="Screenshot 2019-04-30 at 12 27 21" src="https://user-images.githubusercontent.com/5106466/56956641-0908d500-6b45-11e9-8cdc-8c2a34a071e5.png"><img width="50%" alt="Screenshot 2019-04-30 at 12 27 28" src="https://user-images.githubusercontent.com/5106466/56956642-0908d500-6b45-11e9-921c-fabfb8515cc0.png">|<img width="100%" alt="Screenshot 2019-04-30 at 12 26 55" src="https://user-images.githubusercontent.com/5106466/56956650-0efeb600-6b45-11e9-9f5f-f10dd69580d1.png">| There's still a lot of places that are hard to read, but I think this is a good start towards more readable errors. cc cpojer [General][Changed] - Cleanup RedBox message and stack output Pull Request resolved: #24662 Differential Revision: D15147571 Pulled By: cpojer fbshipit-source-id: 1de4e521af988fa7fc709b6accd0ddd984388e72
1 parent b92f30d commit 49d26eb

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

Libraries/Core/ExceptionsManager.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
import type {ExtendedError} from 'parseErrorStack';
1414

15+
const INTERNAL_CALLSITES_REGEX = new RegExp(
16+
[
17+
'/Libraries/Renderer/oss/ReactNativeRenderer-dev\\.js$',
18+
'/Libraries/BatchedBridge/MessageQueue\\.js$',
19+
].join('|'),
20+
);
21+
1522
/**
1623
* Handles the developer-visible aspect of errors and exceptions
1724
*/
@@ -38,9 +45,12 @@ function reportException(e: ExtendedError, isFatal: boolean) {
3845
symbolicateStackTrace(stack)
3946
.then(prettyStack => {
4047
if (prettyStack) {
48+
const stackWithoutInternalCallsites = prettyStack.filter(
49+
frame => frame.file.match(INTERNAL_CALLSITES_REGEX) === null,
50+
);
4151
ExceptionsManager.updateExceptionMessage(
4252
e.message,
43-
prettyStack,
53+
stackWithoutInternalCallsites,
4454
currentExceptionID,
4555
);
4656
} else {

React/Modules/RCTRedBox.m

+15-5
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ - (instancetype)initWithFrame:(CGRect)frame
134134

135135
CGFloat buttonWidth = self.bounds.size.width / 4;
136136
CGFloat bottomButtonHeight = self.bounds.size.height - buttonHeight - [self bottomSafeViewHeight];
137-
137+
138138
dismissButton.frame = CGRectMake(0, bottomButtonHeight, buttonWidth, buttonHeight);
139139
reloadButton.frame = CGRectMake(buttonWidth, bottomButtonHeight, buttonWidth, buttonHeight);
140140
copyButton.frame = CGRectMake(buttonWidth * 2, bottomButtonHeight, buttonWidth, buttonHeight);
@@ -148,11 +148,11 @@ - (instancetype)initWithFrame:(CGRect)frame
148148
[rootView addSubview:copyButton];
149149
[rootView addSubview:extraButton];
150150
[rootView addSubview:topBorder];
151-
151+
152152
UIView *bottomSafeView = [UIView new];
153153
bottomSafeView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
154154
bottomSafeView.frame = CGRectMake(0, self.bounds.size.height - [self bottomSafeViewHeight], self.bounds.size.width, [self bottomSafeViewHeight]);
155-
155+
156156
[rootView addSubview:bottomSafeView];
157157
}
158158
return self;
@@ -176,14 +176,24 @@ - (void)dealloc
176176
[[NSNotificationCenter defaultCenter] removeObserver:self];
177177
}
178178

179+
- (NSString *)stripAnsi:(NSString *)text
180+
{
181+
NSError *error = nil;
182+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\x1b\\[[0-9;]*m" options:NSRegularExpressionCaseInsensitive error:&error];
183+
return [regex stringByReplacingMatchesInString:text options:0 range:NSMakeRange(0, [text length]) withTemplate:@""];
184+
}
185+
179186
- (void)showErrorMessage:(NSString *)message withStack:(NSArray<RCTJSStackFrame *> *)stack isUpdate:(BOOL)isUpdate
180187
{
188+
// Remove ANSI color codes from the message
189+
NSString *messageWithoutAnsi = [self stripAnsi:message];
190+
181191
// Show if this is a new message, or if we're updating the previous message
182-
if ((self.hidden && !isUpdate) || (!self.hidden && isUpdate && [_lastErrorMessage isEqualToString:message])) {
192+
if ((self.hidden && !isUpdate) || (!self.hidden && isUpdate && [_lastErrorMessage isEqualToString:messageWithoutAnsi])) {
183193
_lastStackTrace = stack;
184194
// message is displayed using UILabel, which is unable to render text of
185195
// unlimited length, so we truncate it
186-
_lastErrorMessage = [message substringToIndex:MIN((NSUInteger)10000, message.length)];
196+
_lastErrorMessage = [messageWithoutAnsi substringToIndex:MIN((NSUInteger)10000, messageWithoutAnsi.length)];
187197

188198
[_stackTraceTableView reloadData];
189199

ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
169169
? (TextView) convertView
170170
: (TextView) LayoutInflater.from(parent.getContext())
171171
.inflate(R.layout.redbox_item_title, parent, false);
172-
title.setText(mTitle);
172+
// Remove ANSI color codes from the title
173+
title.setText(mTitle.replaceAll("\\x1b\\[[0-9;]*m", ""));
173174
return title;
174175
} else {
175176
if (convertView == null) {

0 commit comments

Comments
 (0)