From 38b0d57118e6ced506d948036d9915cf263a330c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 3 Nov 2022 10:27:45 -0400 Subject: [PATCH] Improve CoreData error reporting --- .../MastodonCachePersistentStore.swift | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Tusker/CoreData/MastodonCachePersistentStore.swift b/Tusker/CoreData/MastodonCachePersistentStore.swift index 346f0e36..5910a7ea 100644 --- a/Tusker/CoreData/MastodonCachePersistentStore.swift +++ b/Tusker/CoreData/MastodonCachePersistentStore.swift @@ -72,12 +72,27 @@ class MastodonCachePersistentStore: NSPersistentContainer { } do { try context.save() - } catch { + } catch let error as NSError { logger.error("Unable to save managed object context: \(String(describing: error), privacy: .public)") let crumb = Breadcrumb(level: .fatal, category: "PersistentStore") - crumb.message = String(describing: error) + // note: NSDetailedErrorsKey == "NSDetailedErrorsKey" != "NSDetailedErrors" + if let detailed = error.userInfo["NSDetailedErrors"] as? [NSError] { + crumb.data = [ + "errors": detailed.compactMap { error -> [String: Any?]? in + guard let object = error.userInfo[NSValidationObjectErrorKey] as? NSManagedObject else { + return nil + } + return [ + "entity": object.entity.name, + "key": error.userInfo[NSValidationKeyErrorKey], + "value": error.userInfo[NSValidationValueErrorKey], + "message": error.localizedDescription, + ] + } + ] + } SentrySDK.addBreadcrumb(crumb: crumb) - fatalError("Unable to save managed object context") + fatalError("Unable to save managed object context: \(String(describing: error))") } }