Add convenience function for formatting NSError for reporting

Reviewed By: nicklockwood

Differential Revision: D2620933

fb-gh-sync-id: c5c40b78b19f12c9a3b1564b1e21f1acb12309ab
This commit is contained in:
Pieter De Baets 2015-11-05 12:20:02 -08:00 committed by facebook-github-bot-5
parent 31b5b0ac01
commit 824858c6b2
2 changed files with 23 additions and 13 deletions

View File

@ -107,6 +107,11 @@ RCT_EXTERN RCTFatalHandler RCTGetFatalHandler(void);
*/
RCT_EXTERN NSString *RCTCurrentThreadName(void);
/**
* Helper to get generate exception message from NSError
*/
RCT_EXTERN NSString *RCTFormatError(NSString *message, NSArray *stacktrace, NSUInteger maxMessageLength);
/**
* Convenience macro to assert which thread is currently running (DEBUG mode only)
*/

View File

@ -124,22 +124,10 @@ void RCTFatal(NSError *error)
if (fatalHandler) {
fatalHandler(error);
} else {
const NSUInteger maxMessageLength = 75;
NSString *message = [error localizedDescription];
if (message.length > maxMessageLength) {
message = [[message substringToIndex:maxMessageLength] stringByAppendingString:@"..."];
}
NSMutableString *prettyStack = [NSMutableString stringWithString:@"\n"];
if ([error.userInfo[RCTJSStackTraceKey] isKindOfClass:[NSArray class]]) {
for (NSDictionary *frame in error.userInfo[RCTJSStackTraceKey]) {
[prettyStack appendFormat:@"%@@%@:%@\n", frame[@"methodName"], frame[@"lineNumber"], frame[@"column"]];
}
}
#if DEBUG
@try {
#endif
NSString *message = RCTFormatError([error localizedDescription], error.userInfo[RCTJSStackTraceKey], 75);
[NSException raise:@"RCTFatalException" format:@"%@", message];
#if DEBUG
} @catch (NSException *e) {}
@ -156,3 +144,20 @@ RCTFatalHandler RCTGetFatalHandler(void)
{
return RCTCurrentFatalHandler;
}
NSString *RCTFormatError(NSString *message, NSArray *stackTrace, NSUInteger maxMessageLength)
{
if (maxMessageLength > 0 && message.length > maxMessageLength) {
message = [[message substringToIndex:maxMessageLength] stringByAppendingString:@"..."];
}
NSMutableString *prettyStack = [NSMutableString string];
if (stackTrace) {
[prettyStack appendString:@", stack:\n"];
for (NSDictionary *frame in stackTrace) {
[prettyStack appendFormat:@"%@@%@:%@\n", frame[@"methodName"], frame[@"lineNumber"], frame[@"column"]];
}
}
return [NSString stringWithFormat:@"Message: %@%@", message, prettyStack];
}