Skip to content

Commit

Permalink
feat: Add BugsnagCrashReport methods to append metadata (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
kattrali authored May 3, 2017
1 parent 67b6939 commit 29c846e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Source/BugsnagCrashReport.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ NSString *_Nonnull BSGFormatSeverity(BSGSeverity severity);
- (void)attachCustomStacktrace:(NSArray *_Nonnull)frames
withType:(NSString *_Nonnull)type;


/**
* Add metadata to a report to a tab. If the tab does not exist, it will be added.
*
* @param metadata The key/value pairs to add
* @param tabName The name of the report section
*/
- (void)addMetadata:(NSDictionary *_Nonnull)metadata
toTabWithName:(NSString *_Nonnull)tabName;


/**
* Add or remove a value from report metadata. If value is nil, the existing value
* will be removed.
@param attributeName The key name
@param value The value to set
@param tabName The name of the report section
*/
- (void)addAttribute:(NSString*_Nonnull)attributeName
withValue:(id _Nullable)value
toTabWithName:(NSString*_Nonnull)tabName;

/**
* The release stages used to notify at the time this report is captured
*/
Expand Down
25 changes: 25 additions & 0 deletions Source/BugsnagCrashReport.m
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ BSGSeverity BSGParseSeverity(NSString *severity) {

static NSString *const DEFAULT_EXCEPTION_TYPE = @"cocoa";

@interface NSDictionary (BSGKSMerge)
- (NSDictionary*)BSG_mergedInto:(NSDictionary *)dest;
@end

@interface BugsnagCrashReport ()

/**
Expand Down Expand Up @@ -293,6 +297,27 @@ - (instancetype)initWithErrorName:(NSString *)name
return self;
}

- (void)addMetadata:(NSDictionary *)tabData toTabWithName:(NSString *)tabName {
NSMutableDictionary *allMetadata = [self.metaData mutableCopy];
NSMutableDictionary *allTabData = allMetadata[tabName] ?: [NSMutableDictionary new];
allMetadata[tabName] = [tabData BSG_mergedInto:allTabData];
self.metaData = allMetadata;
}

- (void)addAttribute:(NSString*)attributeName
withValue:(id)value
toTabWithName:(NSString*)tabName {
NSMutableDictionary *allMetadata = [self.metaData mutableCopy];
NSMutableDictionary *allTabData = allMetadata[tabName] ?: [NSMutableDictionary new];
if (value) {
allTabData[attributeName] = value;
} else {
[allTabData removeObjectForKey:attributeName];
}
allMetadata[tabName] = allTabData;
self.metaData = allMetadata;
}

- (BOOL)shouldBeSent {
return [self.notifyReleaseStages containsObject:self.releaseStage]
|| (self.notifyReleaseStages.count == 0 && [[Bugsnag configuration] shouldSendReports]);
Expand Down
41 changes: 41 additions & 0 deletions Tests/BugsnagCrashReportTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,47 @@ - (void)testReadNotifyReleaseStagesSends {
XCTAssertTrue([self.report shouldBeSent]);
}

- (void)testAddMetadataAddsNewTab {
NSDictionary *metadata = @{ @"color": @"blue", @"beverage": @"tea" };
[self.report addMetadata:metadata toTabWithName:@"user prefs"];
NSDictionary *prefs = self.report.metaData[@"user prefs"];
XCTAssertEqual(@"blue", prefs[@"color"]);
XCTAssertEqual(@"tea", prefs[@"beverage"]);
XCTAssert([prefs count] == 2);
}

- (void)testAddMetadataMergesExistingTab {
NSDictionary *oldMetadata = @{ @"color": @"red", @"food": @"carrots" };
[self.report addMetadata:oldMetadata toTabWithName:@"user prefs"];
NSDictionary *metadata = @{ @"color": @"blue", @"beverage": @"tea" };
[self.report addMetadata:metadata toTabWithName:@"user prefs"];
NSDictionary *prefs = self.report.metaData[@"user prefs"];
XCTAssertEqual(@"blue", prefs[@"color"]);
XCTAssertEqual(@"tea", prefs[@"beverage"]);
XCTAssertEqual(@"carrots", prefs[@"food"]);
XCTAssert([prefs count] == 3);
}

- (void)testAddAttributeAddsNewTab {
[self.report addAttribute:@"color" withValue:@"blue" toTabWithName:@"prefs"];
NSDictionary *prefs = self.report.metaData[@"prefs"];
XCTAssertEqual(@"blue", prefs[@"color"]);
}

- (void)testAddAttributeOverridesExistingValue {
[self.report addAttribute:@"color" withValue:@"red" toTabWithName:@"prefs"];
[self.report addAttribute:@"color" withValue:@"blue" toTabWithName:@"prefs"];
NSDictionary *prefs = self.report.metaData[@"prefs"];
XCTAssertEqual(@"blue", prefs[@"color"]);
}

- (void)testAddAttributeRemovesValue {
[self.report addAttribute:@"color" withValue:@"red" toTabWithName:@"prefs"];
[self.report addAttribute:@"color" withValue:nil toTabWithName:@"prefs"];
NSDictionary *prefs = self.report.metaData[@"prefs"];
XCTAssertNil(prefs[@"color"]);
}

- (void)testNotifyReleaseStagesSendsFromConfig {
BugsnagConfiguration *config = [BugsnagConfiguration new];
config.notifyReleaseStages = @[@"foo"];
Expand Down

0 comments on commit 29c846e

Please sign in to comment.