Skip to content

Commit

Permalink
Merge pull request #84 from zeroseven/truncate_crash_report
Browse files Browse the repository at this point in the history
truncate crash log string to 60000 characters
  • Loading branch information
kyrylo committed Jul 11, 2017
2 parents 91622d0 + bc4bc6f commit 6fc4456
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions Airbrake/notifier/ABNotifier.m
Original file line number Diff line number Diff line change
Expand Up @@ -525,15 +525,33 @@ + (void)postNoticesWithPaths:(NSArray *)paths {
+ (NSData *)JSONString:(NSString *)filePath {
NSData *jsonData;
NSError *error = NULL;
NSError *jsonSerializationError = nil;

NSString *dataStr = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

if (!dataStr) {
jsonData = nil;
ABLog(@"ERROR: Crash report data is not readable.");
return jsonData;
}
NSDictionary *notice = @{@"report": dataStr, @"context":@{@"userName":__userName, @"environment":__envName, @"notifier":@{@"name":ABNotifierName,@"version":ABNotifierVersion,@"url":@"https://github.com/airbrake/airbrake-ios"}}};
jsonData = [NSJSONSerialization dataWithJSONObject:notice options:NSJSONWritingPrettyPrinted error:&jsonSerializationError];

jsonData = [self JSONObjectWithPayload:dataStr];

int maxSize = 64*1024;

if(jsonData.length > maxSize) {
// truncate data string
dataStr = [dataStr substringWithRange:NSMakeRange(0, dataStr.length - (jsonData.length - maxSize))];
jsonData = [self JSONObjectWithPayload:dataStr];
}

return jsonData;
}

+(NSData*) JSONObjectWithPayload:(NSString*)payload {

NSDictionary *notice = @{@"report": payload, @"context":@{@"userName":__userName, @"environment":__envName, @"notifier":@{@"name":ABNotifierName,@"version":ABNotifierVersion,@"url":@"https://github.com/airbrake/airbrake-ios"}}};
NSError *jsonSerializationError = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:notice options:NSJSONWritingPrettyPrinted error:&jsonSerializationError];
if(jsonSerializationError) {
jsonData = nil;
ABLog(@"ERROR: JSON Encoding Failed: %@", [jsonSerializationError localizedDescription]);
Expand Down

0 comments on commit 6fc4456

Please sign in to comment.