Skip to content

Commit 2831d9e

Browse files
Zack Sheppardfacebook-github-bot
Zack Sheppard
authored andcommitted
Extend reason message for RCTFatalException (#22532)
Summary: Fixes #22530 As described in the issue, the previous behavior for the `RCTFatal` macro was to truncate the `reason` on the resulting `NSException` to 75 characters. This would ensure the reason would fit on a single line, but resulted in issues debugging errors that occurred in the wild, as many crash logging tools (like Sentry) discard the `name` value of the exception and use the `reason` as their primary identifier. At 75 characters, useful information like the location of the error would usually be truncated. - [x] This extends the truncation threshold to 175 characters, which should be short enough to prevent full-screen-takeover length errors, but long enough to provide useful context to the error. - [x] This adds a `userInfo` value to the resulting `NSException`. It copies over the `userInfo` from the `NSError` passed to the macro, and adds an "untruncated message" value that contains the untruncated version of the `NSException`'s reason. [iOS] [Changed] - RCTFatalExceptions now include more information in their reason and a userInfo. <!-- CATEGORY may be: - [General] - [iOS] - [Android] TYPE may be: - [Added] for new features. - [Changed] for changes in existing functionality. - [Deprecated] for soon-to-be removed features. - [Removed] for now removed features. - [Fixed] for any bug fixes. - [Security] in case of vulnerabilities. For more detail, see https://keepachangelog.com/en/1.0.0/#how MESSAGE may answer "what and why" on a feature level. Use this to briefly tell React Native users about notable changes. EXAMPLES: [General] [Added] - Add snapToOffsets prop to ScrollView component [General] [Fixed] - Fix various issues in snapToInterval on ScrollView component [iOS] [Fixed] - Fix crash in RCTImagePicker --> Pull Request resolved: #22532 Differential Revision: D13373469 Pulled By: cpojer fbshipit-source-id: ac140d14ce76e1664869437c2c178bdd65ab6e0e
1 parent 983ddc7 commit 2831d9e

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

React/Base/RCTAssert.m

+15-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
NSString *const RCTJSStackTraceKey = @"RCTJSStackTraceKey";
1313
NSString *const RCTJSRawStackTraceKey = @"RCTJSRawStackTraceKey";
1414
NSString *const RCTFatalExceptionName = @"RCTFatalException";
15+
NSString *const RCTUntruncatedMessageKey = @"RCTUntruncatedMessageKey";
1516

1617
static NSString *const RCTAssertFunctionStack = @"RCTAssertFunctionStack";
1718

@@ -128,8 +129,20 @@ void RCTFatal(NSError *error)
128129
@try {
129130
#endif
130131
NSString *name = [NSString stringWithFormat:@"%@: %@", RCTFatalExceptionName, error.localizedDescription];
131-
NSString *message = RCTFormatError(error.localizedDescription, error.userInfo[RCTJSStackTraceKey], 75);
132-
@throw [[NSException alloc] initWithName:name reason:message userInfo:nil];
132+
133+
// Truncate the localized description to 175 characters to avoid wild screen overflows
134+
NSString *message = RCTFormatError(error.localizedDescription, error.userInfo[RCTJSStackTraceKey], 175);
135+
136+
// Attach an untruncated copy of the description to the userInfo, in case it is needed
137+
NSMutableDictionary *userInfo = [error.userInfo mutableCopy];
138+
[userInfo setObject:RCTFormatError(error.localizedDescription, error.userInfo[RCTJSStackTraceKey], -1)
139+
forKey:RCTUntruncatedMessageKey];
140+
141+
// Expected resulting exception information:
142+
// name: RCTFatalException: <underlying error description>
143+
// reason: <underlying error description plus JS stack trace, truncated to 175 characters>
144+
// userInfo: <underlying error userinfo, plus untruncated description plus JS stack trace>
145+
@throw [[NSException alloc] initWithName:name reason:message userInfo:userInfo];
133146
#if DEBUG
134147
} @catch (NSException *e) {}
135148
#endif

0 commit comments

Comments
 (0)