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

Always throw an exception for a core data error that prevents us from initializing Tracks. #198

Merged
merged 1 commit into from
Mar 10, 2022
Merged
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
37 changes: 12 additions & 25 deletions Sources/Model/ObjC/Common/Core Data/TracksContextManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,17 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
NSURL *storeURL = [self storeURL];
NSError *error = nil;
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {

// Delete the store and try again
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:nil];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
TracksLogError(@"Unresolved error %@, %@", error, [error userInfo]);

if (self.shouldThrowUponFatalCondition) {
@throw [NSException exceptionWithName:TracksPersistentStoreException
reason:[NSString stringWithFormat:@"Error initializing Tracks: %@", error]
userInfo:error.userInfo];
} else {
abort();
}

@throw [NSException exceptionWithName:TracksPersistentStoreException
reason:[NSString stringWithFormat:@"Error initializing Tracks: %@", error]
userInfo:error.userInfo];
}
}

Expand All @@ -88,30 +84,21 @@ - (NSURL *)applicationSupportURLForAppWithBundleIdentifier:(NSString *)bundleIde
withIntermediateDirectories:true
attributes:nil
error:&error];

// It seems safe not to handle this error because Application Support should always be
// available and one should always be able to create a folder in it
if (error != nil) {
TracksLogError(@"Failed to create folder for %@ in Application Support: %@, %@", bundleIdentifier, error, [error userInfo]);

if (self.shouldThrowUponFatalCondition) {
@throw [NSException exceptionWithName:TracksApplicationSupportException
reason:[NSString stringWithFormat:@"Error creating the ApplicationSupport Folder: %@", error]
userInfo:error.userInfo];
} else {
abort();
}

@throw [NSException exceptionWithName:TracksApplicationSupportException
reason:[NSString stringWithFormat:@"Error creating the ApplicationSupport Folder: %@", error]
userInfo:error.userInfo];

}

return folder;
}

- (BOOL)shouldThrowUponFatalCondition {
// We consider it safe to Throw (whenever a Fatal Condition arises) whenever there is no global
// NSException handler set
return NSGetUncaughtExceptionHandler() == nil;
}

// Application Support contains "the files that your app creates and manages on behalf of the user
// and can include files that contain user data".
//
Expand Down