FilesystemError.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. enum IONFileMethod: String {
  2. case readFile
  3. case readFileInChunks
  4. case writeFile
  5. case appendFile
  6. case deleteFile
  7. case mkdir
  8. case rmdir
  9. case readdir
  10. case stat
  11. case getUri
  12. case rename
  13. case copy
  14. }
  15. enum FilesystemError: Error {
  16. case bridgeNotInitialised
  17. case invalidInput(method: IONFileMethod)
  18. case invalidPath(_ path: String)
  19. case fileNotFound(method: IONFileMethod, _ path: String)
  20. case directoryAlreadyExists(_ path: String)
  21. case parentDirectoryMissing
  22. case cannotDeleteChildren
  23. case operationFailed(method: IONFileMethod, _ error: Error)
  24. func toCodeMessagePair() -> (code: String, message: String) {
  25. ("OS-PLUG-FILE-\(String(format: "%04d", code))", description)
  26. }
  27. }
  28. private extension FilesystemError {
  29. var code: Int {
  30. switch self {
  31. case .bridgeNotInitialised: 4
  32. case .invalidInput: 5
  33. case .invalidPath: 6
  34. case .fileNotFound: 8
  35. case .directoryAlreadyExists: 10
  36. case .parentDirectoryMissing: 11
  37. case .cannotDeleteChildren: 12
  38. case .operationFailed: 13
  39. }
  40. }
  41. var description: String {
  42. switch self {
  43. case .bridgeNotInitialised: "Capacitor bridge isn't initialized."
  44. case .invalidInput(let method): "The '\(method.rawValue)' input parameters aren't valid."
  45. case .invalidPath(let path): "Invalid \(!path.isEmpty ? "'" + path + "' " : "")path."
  46. case .fileNotFound(let method, let path): "'\(method.rawValue)' failed because file\(!path.isEmpty ? " at '" + path + "' " : "") does not exist."
  47. case .directoryAlreadyExists(let path): "Directory\(!path.isEmpty ? " at '" + path + "' " : "") already exists, cannot be overwritten."
  48. case .parentDirectoryMissing: "Missing parent directory - possibly recursive=false was passed or parent directory creation failed."
  49. case .cannotDeleteChildren: "Cannot delete directory with children; received recursive=false but directory has contents."
  50. case .operationFailed(let method, let error): "'\(method.rawValue)' failed with: \(error.localizedDescription)"
  51. }
  52. }
  53. }