CoreDataError.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Foundation
  2. enum CoreDataError: Error {
  3. case validationError(function: String, file: String)
  4. case creationError(function: String, file: String)
  5. case batchInsertError(function: String, file: String)
  6. case batchDeleteError(function: String, file: String)
  7. case persistentHistoryChangeError(function: String, file: String)
  8. case unexpectedError(error: Error, function: String, file: String)
  9. case fetchError(function: String, file: String)
  10. case storeNotInitializedError(function: String, file: String)
  11. }
  12. extension CoreDataError: LocalizedError {
  13. var errorDescription: String? {
  14. switch self {
  15. case let .creationError(function, file):
  16. return NSLocalizedString("Failed to create a new object in \(function) from \(file).", comment: "")
  17. case let .batchInsertError(function, file):
  18. return NSLocalizedString("Failed to execute a batch insert request in \(function) from \(file).", comment: "")
  19. case let .batchDeleteError(function, file):
  20. return NSLocalizedString("Failed to execute a batch delete request in \(function) from \(file).", comment: "")
  21. case let .persistentHistoryChangeError(function, file):
  22. return NSLocalizedString(
  23. "Failed to execute a persistent history change request in \(function) from \(file).",
  24. comment: ""
  25. )
  26. case let .unexpectedError(error, function, file):
  27. return NSLocalizedString(
  28. "Received unexpected error in \(function) from \(file): \(error.localizedDescription)",
  29. comment: ""
  30. )
  31. case let .fetchError(function, file):
  32. return NSLocalizedString(
  33. "Failed to fetch object \(DebuggingIdentifiers.failed) in \(function) from \(file).",
  34. comment: ""
  35. )
  36. case let .validationError(function, file):
  37. return NSLocalizedString("Failed to validate object in \(function) from \(file).", comment: "")
  38. case let .storeNotInitializedError(function, file):
  39. return NSLocalizedString(
  40. "Store not initialized in \(function) from \(file).",
  41. comment: ""
  42. )
  43. }
  44. }
  45. }
  46. extension CoreDataError: Identifiable {
  47. var id: String? {
  48. errorDescription
  49. }
  50. }