Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove magic values #792

Merged
merged 1 commit into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions Bugsnag/BSGOutOfMemoryWatchdog.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "Private.h"
#import "BugsnagErrorTypes.h"
#import "BSG_RFC3339DateTool.h"
#import "BugsnagKeys.h"

@interface BSGOutOfMemoryWatchdog ()
@property(nonatomic, getter=isWatching) BOOL watching;
Expand Down Expand Up @@ -124,32 +125,32 @@ - (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSString *, id> *)change
context:(void *)context {
self.cachedFileInfo[@"app"][@"releaseStage"] = change[NSKeyValueChangeNewKey];
self.cachedFileInfo[BSGKeyApp][BSGKeyReleaseStage] = change[NSKeyValueChangeNewKey];
[self writeSentinelFile];
}

- (void)handleTransitionToActive:(NSNotification *)note {
self.cachedFileInfo[@"app"][@"isActive"] = @YES;
self.cachedFileInfo[BSGKeyApp][@"isActive"] = @YES;
[self writeSentinelFile];
}

- (void)handleTransitionToInactive:(NSNotification *)note {
self.cachedFileInfo[@"app"][@"isActive"] = @NO;
self.cachedFileInfo[BSGKeyApp][@"isActive"] = @NO;
[self writeSentinelFile];
}

- (void)handleTransitionToForeground:(NSNotification *)note {
self.cachedFileInfo[@"app"][@"inForeground"] = @YES;
self.cachedFileInfo[BSGKeyApp][@"inForeground"] = @YES;
[self writeSentinelFile];
}

- (void)handleTransitionToBackground:(NSNotification *)note {
self.cachedFileInfo[@"app"][@"inForeground"] = @NO;
self.cachedFileInfo[BSGKeyApp][@"inForeground"] = @NO;
[self writeSentinelFile];
}

- (void)handleLowMemoryChange:(NSNotification *)note {
self.cachedFileInfo[@"device"][@"lowMemory"] = [BSG_RFC3339DateTool
self.cachedFileInfo[BSGKeyDevice][@"lowMemory"] = [BSG_RFC3339DateTool
stringFromDate:[NSDate date]];
[self writeSentinelFile];
}
Expand All @@ -158,16 +159,16 @@ - (void)handleUpdateSession:(NSNotification *)note {
id session = [note object];
NSMutableDictionary *cache = (id)self.cachedFileInfo;
if (session) {
cache[@"session"] = session;
cache[BSGKeySession] = session;
} else {
[cache removeObjectForKey:@"session"];
[cache removeObjectForKey:BSGKeySession];
}
[self writeSentinelFile];
}

- (void)setCodeBundleId:(NSString *)codeBundleId {
_codeBundleId = codeBundleId;
self.cachedFileInfo[@"app"][@"codeBundleId"] = codeBundleId;
self.cachedFileInfo[BSGKeyApp][BSGKeyCodeBundleId] = codeBundleId;

if ([self isWatching]) {
[self writeSentinelFile];
Expand Down Expand Up @@ -252,13 +253,13 @@ - (NSMutableDictionary *)generateCacheInfoWithConfig:(BugsnagConfiguration *)con
NSMutableDictionary *cache = [NSMutableDictionary new];
NSMutableDictionary *app = [NSMutableDictionary new];

app[@"id"] = systemInfo[@BSG_KSSystemField_BundleID] ?: @"";
app[@"name"] = systemInfo[@BSG_KSSystemField_BundleName] ?: @"";
app[@"releaseStage"] = config.releaseStage;
app[@"version"] = systemInfo[@BSG_KSSystemField_BundleShortVersion] ?: @"";
app[@"bundleVersion"] = systemInfo[@BSG_KSSystemField_BundleVersion] ?: @"";
app[BSGKeyId] = systemInfo[@BSG_KSSystemField_BundleID] ?: @"";
app[BSGKeyName] = systemInfo[@BSG_KSSystemField_BundleName] ?: @"";
app[BSGKeyReleaseStage] = config.releaseStage;
app[BSGKeyVersion] = systemInfo[@BSG_KSSystemField_BundleShortVersion] ?: @"";
app[BSGKeyBundleVersion] = systemInfo[@BSG_KSSystemField_BundleVersion] ?: @"";
// 'codeBundleId' only (optionally) exists for React Native clients and defaults otherwise to nil
app[@"codeBundleId"] = self.codeBundleId;
app[BSGKeyCodeBundleId] = self.codeBundleId;
#if BSGOOMAvailable
UIApplicationState state = [BSG_KSSystemInfo currentAppState];
app[@"inForeground"] = @([BSG_KSSystemInfo isInForeground:state]);
Expand All @@ -267,11 +268,11 @@ - (NSMutableDictionary *)generateCacheInfoWithConfig:(BugsnagConfiguration *)con
app[@"inForeground"] = @YES;
#endif
#if BSG_PLATFORM_TVOS
app[@"type"] = @"tvOS";
app[BSGKeyType] = @"tvOS";
#elif BSG_PLATFORM_IOS
app[@"type"] = @"iOS";
app[BSGKeyType] = @"iOS";
#endif
cache[@"app"] = app;
cache[BSGKeyApp] = app;

NSMutableDictionary *device = [NSMutableDictionary new];
device[@"id"] = systemInfo[@BSG_KSSystemField_DeviceAppHash];
Expand All @@ -289,7 +290,7 @@ - (NSMutableDictionary *)generateCacheInfoWithConfig:(BugsnagConfiguration *)con
#else
device[@"simulator"] = @NO;
#endif
cache[@"device"] = device;
cache[BSGKeyDevice] = device;

return cache;
}
Expand Down
1 change: 0 additions & 1 deletion Bugsnag/Helpers/BugsnagKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ extern NSString *const BSGDefaultNotifyUrl;
extern NSString *const BSGKeyAction;
extern NSString *const BSGKeyApiKey;
extern NSString *const BSGKeyApp;
extern NSString *const BSGKeyAppState;
extern NSString *const BSGKeyAppType;
extern NSString *const BSGKeyAppVersion;
extern NSString *const BSGKeyAttributes;
Expand Down
1 change: 0 additions & 1 deletion Bugsnag/Helpers/BugsnagKeys.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
NSString *const BSGKeyAction = @"action";
NSString *const BSGKeyApiKey = @"apiKey";
NSString *const BSGKeyApp = @"app";
NSString *const BSGKeyAppState = @"app";
NSString *const BSGKeyAppType = @"appType";
NSString *const BSGKeyAppVersion = @"appVersion";
NSString *const BSGKeyAttributes = @"attributes";
Expand Down
34 changes: 17 additions & 17 deletions Bugsnag/Payload/BugsnagEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ id BSGLoadConfigValue(NSDictionary *report, NSString *valueName) {
if ([context isKindOfClass:[NSString class]]) {
return context;
}
context = BSGLoadConfigValue(report, @"context");
context = BSGLoadConfigValue(report, BSGKeyContext);
if ([context isKindOfClass:[NSString class]]) {
return context;
}
Expand Down Expand Up @@ -392,8 +392,8 @@ - (instancetype)initWithOOMData:(NSDictionary *)event {
}
BugsnagMetadata *metadata = [BugsnagMetadata new];
// Cocoa-specific, non-spec., device and app data
[metadata addMetadata:BSGParseDeviceMetadata(event) toSection:@"device"];
[metadata addMetadata:BSGParseAppMetadata(event) toSection:@"app"];
[metadata addMetadata:BSGParseDeviceMetadata(event) toSection:BSGKeyDevice];
[metadata addMetadata:BSGParseAppMetadata(event) toSection:BSGKeyApp];

BugsnagEvent *obj = [self initWithApp:[BugsnagAppWithState appWithOomData:[event valueForKeyPath:@"user.state.oom.app"]]
device:[BugsnagDeviceWithState deviceWithOomData:[event valueForKeyPath:@"user.state.oom.device"]]
Expand Down Expand Up @@ -433,8 +433,8 @@ - (instancetype)initWithKSCrashData:(NSDictionary *)event {
}

// Cocoa-specific, non-spec., device and app data
[metadata addMetadata:BSGParseDeviceMetadata(event) toSection:@"device"];
[metadata addMetadata:BSGParseAppMetadata(event) toSection:@"app"];
[metadata addMetadata:BSGParseDeviceMetadata(event) toSection:BSGKeyDevice];
[metadata addMetadata:BSGParseAppMetadata(event) toSection:BSGKeyApp];

NSDictionary *recordedState = [event valueForKeyPath:@"user.handledState"];

Expand All @@ -445,8 +445,8 @@ - (instancetype)initWithKSCrashData:(NSDictionary *)event {
depth = 0;
}
BugsnagSession *session;
if (event[@"user"][@"id"]) {
session = [[BugsnagSession alloc] initWithDictionary:event[@"user"]];
if (event[BSGKeyUser][@"id"]) {
session = [[BugsnagSession alloc] initWithDictionary:event[BSGKeyUser]];
}

// generate threads/error info
Expand Down Expand Up @@ -547,20 +547,20 @@ - (instancetype)initWithUserData:(NSDictionary *)event {
}
}
}
BugsnagSession *session = [BugsnagSession fromJson:bugsnagPayload[@"session"]];
BugsnagSession *session = [BugsnagSession fromJson:bugsnagPayload[BSGKeySession]];

BugsnagEvent *obj = [self initWithApp:[BugsnagAppWithState appFromJson:bugsnagPayload[@"app"]]
device:[BugsnagDeviceWithState deviceFromJson:bugsnagPayload[@"device"]]
BugsnagEvent *obj = [self initWithApp:[BugsnagAppWithState appFromJson:bugsnagPayload[BSGKeyApp]]
device:[BugsnagDeviceWithState deviceFromJson:bugsnagPayload[BSGKeyDevice]]
handledState:[BugsnagHandledState handledStateFromJson:bugsnagPayload]
user:[[BugsnagUser alloc] initWithDictionary:bugsnagPayload[@"user"]]
metadata:[[BugsnagMetadata alloc] initWithDictionary:bugsnagPayload[@"metaData"]]
breadcrumbs:[BugsnagBreadcrumb breadcrumbArrayFromJson:bugsnagPayload[@"breadcrumbs"]]
user:[[BugsnagUser alloc] initWithDictionary:bugsnagPayload[BSGKeyUser]]
metadata:[[BugsnagMetadata alloc] initWithDictionary:bugsnagPayload[BSGKeyMetadata]]
breadcrumbs:[BugsnagBreadcrumb breadcrumbArrayFromJson:bugsnagPayload[BSGKeyBreadcrumbs]]
errors:errors
threads:threads
session:session];
obj.apiKey = bugsnagPayload[@"apiKey"];
obj.context = bugsnagPayload[@"context"];
obj.groupingHash = bugsnagPayload[@"groupingHash"];
obj.apiKey = bugsnagPayload[BSGKeyApiKey];
obj.context = bugsnagPayload[BSGKeyContext];
obj.groupingHash = bugsnagPayload[BSGKeyGroupingHash];
obj.error = [self getMetadataFromSection:BSGKeyError];

if ([errors count] > 0) {
Expand All @@ -571,7 +571,7 @@ - (instancetype)initWithUserData:(NSDictionary *)event {
}

- (NSMutableDictionary *)parseOnCrashData:(NSDictionary *)report {
NSMutableDictionary *userAtCrash = [report[@"user"] mutableCopy];
NSMutableDictionary *userAtCrash = [report[BSGKeyUser] mutableCopy];
// avoid adding internal information to user-defined metadata
NSArray *blacklistedKeys = @[
@BSG_KSCrashField_Overrides,
Expand Down
4 changes: 2 additions & 2 deletions Bugsnag/Payload/BugsnagSessionTrackingPayload.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ - (NSMutableDictionary *)toJson
// app/device data collection relies on KSCrash reports,
// need to mimic the JSON structure here
BugsnagApp *app = self.sessions[0].app;
BSGDictSetSafeObject(dict, [app toDict], @"app");
BSGDictSetSafeObject(dict, [app toDict], BSGKeyApp);

BugsnagDevice *device = self.sessions[0].device;
BSGDictSetSafeObject(dict, [device toDictionary], @"device");
BSGDictSetSafeObject(dict, [device toDictionary], BSGKeyDevice);
return dict;
}

Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog

### Bug fixes

* Removed some magic values and used the const string references instead.
[#792](https://github.com/bugsnag/bugsnag-cocoa/pull/792)

* Guard against non-string metadata map keys
[#790](https://bugsnag.atlassian.net/browse/PLAT-4685)

Expand Down
3 changes: 3 additions & 0 deletions Tests/BugsnagErrorReportSinkTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ - (void)testCorrectTopLevelKeys {
}

- (void)testCorrectNotifierKeys {
NSLog(@"### processedData = %@", self.processedData);
NSLog(@"### top keys = %@", [self.processedData allKeys]);
NSLog(@"### notifier = %@", self.processedData[@"notifier"]);
NSArray *expectedKeys = @[ @"name", @"url", @"version" ];
NSArray *notifierKeys = [self.processedData[@"notifier"] allKeys];
XCTAssertEqualObjects(
Expand Down