Improve CoreData error reporting

This commit is contained in:
Shadowfacts 2022-11-03 10:27:45 -04:00
parent b38c24b347
commit 38b0d57118
1 changed files with 18 additions and 3 deletions

View File

@ -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))")
}
}