From bca28c999bca5a567e535db70b7bf91c4c3bffc1 Mon Sep 17 00:00:00 2001 From: Michelle Tessier Date: Mon, 7 Mar 2022 12:16:18 -0700 Subject: [PATCH] Always throw a specific exception for these error cases. It appears that in some cases, NSGetUncaughtExceptionHandler is not nil when we expect it to be, and thus we are unable to get information about why the data model is not able to set up correctly --- .../Common/Core Data/TracksContextManager.m | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/Sources/Model/ObjC/Common/Core Data/TracksContextManager.m b/Sources/Model/ObjC/Common/Core Data/TracksContextManager.m index 3b59c64d..16751f42 100644 --- a/Sources/Model/ObjC/Common/Core Data/TracksContextManager.m +++ b/Sources/Model/ObjC/Common/Core Data/TracksContextManager.m @@ -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]; } } @@ -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". //