CoreDataError.swift 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. }
  11. extension CoreDataError: LocalizedError {
  12. var errorDescription: String? {
  13. switch self {
  14. case let .creationError(function, file):
  15. return NSLocalizedString("Failed to create a new object in \(function) from \(file).", comment: "")
  16. case let .batchInsertError(function, file):
  17. return NSLocalizedString("Failed to execute a batch insert request in \(function) from \(file).", comment: "")
  18. case let .batchDeleteError(function, file):
  19. return NSLocalizedString("Failed to execute a batch delete request in \(function) from \(file).", comment: "")
  20. case let .persistentHistoryChangeError(function, file):
  21. return NSLocalizedString(
  22. "Failed to execute a persistent history change request in \(function) from \(file).",
  23. comment: ""
  24. )
  25. case let .unexpectedError(error, function, file):
  26. return NSLocalizedString(
  27. "Received unexpected error in \(function) from \(file): \(error.localizedDescription)",
  28. comment: ""
  29. )
  30. case let .fetchError(function, file):
  31. return NSLocalizedString(
  32. "Failed to fetch object \(DebuggingIdentifiers.failed) in \(function) from \(file).",
  33. comment: ""
  34. )
  35. case let .validationError(function, file):
  36. return NSLocalizedString("Failed to validate object in \(function) from \(file).", comment: "")
  37. }
  38. }
  39. }
  40. extension CoreDataError: Identifiable {
  41. var id: String? {
  42. errorDescription
  43. }
  44. }