Skip to content

Commit

Permalink
[iOS] Modify logError() to send stacktrace.js output as an actual sta…
Browse files Browse the repository at this point in the history
…cktrace instead of custom keys. Relates to #118.
  • Loading branch information
Dave Alden committed Nov 1, 2019
1 parent 0035dac commit 6319101
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/ios/FirebasePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -540,26 +540,38 @@ - (void)logEvent:(CDVInvokedUrlCommand *)command {
- (void)logError:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* errorMessage = [command.arguments objectAtIndex:0];
NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] init];

CDVCommandStatus status = CDVCommandStatus_OK;

@try {
// We can optionally be passed a stack trace from stackTrace.js which we'll put in userInfo.
if ([command.arguments count] > 1) {
NSArray *stack = [command.arguments objectAtIndex:1];
int lineNum = 1;
for (NSDictionary *entry in stack) {
NSString *key = [NSString stringWithFormat:@"Stack_line_%02d", lineNum++];
userInfo[key] = entry[@"source"];
NSArray* stackFrames = [command.arguments objectAtIndex:1];

NSString* message = errorMessage;
NSString* name = @"Uncaught Javascript exception";
NSMutableArray *customFrames = [[NSMutableArray alloc] init];

for (NSDictionary* stackFrame in stackFrames) {
CLSStackFrame *customFrame = [CLSStackFrame stackFrame];
[customFrame setSymbol:stackFrame[@"functionName"]];
[customFrame setFileName:stackFrame[@"fileName"]];
[customFrame setLibrary:stackFrame[@"source"]];
[customFrame setOffset:(uint64_t) [stackFrame[@"columnNumber"] intValue]];
[customFrame setLineNumber:(uint32_t) [stackFrame[@"lineNumber"] intValue]];
[customFrames addObject:customFrame];
}
[[Crashlytics sharedInstance] recordCustomExceptionName:name reason:message frameArray:customFrames];
}else{
//TODO detect and handle non-stack userInfo and pass to recordError
NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] init];
NSError *error = [NSError errorWithDomain:errorMessage code:0 userInfo:userInfo];
[CrashlyticsKit recordError:error];
}
} @catch (NSException *exception) {
CLSNSLog(@"Exception in logError: %@, original error: %@", exception.description, errorMessage);
status = CDVCommandStatus_ERROR;
}

NSError *error = [NSError errorWithDomain:errorMessage code:0 userInfo:userInfo];
[CrashlyticsKit recordError:error];

CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:status];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
Expand Down

0 comments on commit 6319101

Please sign in to comment.