FileKitError.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // FileKitErrorType.swift
  3. // FileKit
  4. //
  5. // The MIT License (MIT)
  6. //
  7. // Copyright (c) 2015-2017 Nikolai Vazquez
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. //
  27. import Foundation
  28. // MARK: FileKitError
  29. /// An error that can be thrown by FileKit.
  30. public enum FileKitError: Error {
  31. /// A file does not exist.
  32. case fileDoesNotExist(path: Path)
  33. /// A file already exists at operation destination.
  34. case fileAlreadyExists(path: Path)
  35. /// Could not change the current directory.
  36. case changeDirectoryFail(from: Path, to: Path, error: Error)
  37. /// A symbolic link could not be created.
  38. case createSymlinkFail(from: Path, to: Path, error: Error)
  39. /// A hard link could not be created.
  40. case createHardlinkFail(from: Path, to: Path, error: Error)
  41. /// A file could not be created.
  42. case createFileFail(path: Path)
  43. /// A directory could not be created.
  44. case createDirectoryFail(path: Path, error: Error)
  45. /// A file could not be deleted.
  46. case deleteFileFail(path: Path, error: Error)
  47. /// A file could not be read from.
  48. case readFromFileFail(path: Path, error: Error)
  49. /// A file could not be written to.
  50. case writeToFileFail(path: Path, error: Error)
  51. /// A file could not be moved.
  52. case moveFileFail(from: Path, to: Path, error: Error)
  53. /// A file could not be copied.
  54. case copyFileFail(from: Path, to: Path, error: Error)
  55. /// One or many attributes could not be changed.
  56. case attributesChangeFail(path: Path, error: Error)
  57. // MARK: - Reason
  58. /// An error that could be cause of `FileKitError`
  59. enum ReasonError: Error {
  60. /// Failed to read or convert to specific type.
  61. case conversion(Any)
  62. /// A file stream/handle is alread closed.
  63. case closed
  64. /// Failed to encode string using specific encoding.
  65. case encoding(String.Encoding, data: String)
  66. }
  67. }
  68. // MARK: - Message
  69. extension FileKitError {
  70. /// The reason for why the error occured.
  71. public var message: String {
  72. switch self {
  73. case let .fileDoesNotExist(path):
  74. return "File does not exist at \"\(path)\""
  75. case let .fileAlreadyExists(path):
  76. return "File already exists at \"\(path)\""
  77. case let .changeDirectoryFail(fromPath, toPath, _):
  78. return "Could not change the directory from \"\(fromPath)\" to \"\(toPath)\""
  79. case let .createSymlinkFail(fromPath, toPath, _):
  80. return "Could not create symlink from \"\(fromPath)\" to \"\(toPath)\""
  81. case let .createHardlinkFail(fromPath, toPath, _):
  82. return "Could not create a hard link from \"\(fromPath)\" to \"\(toPath)\""
  83. case let .createFileFail(path):
  84. return "Could not create file at \"\(path)\""
  85. case let .createDirectoryFail(path, _):
  86. return "Could not create a directory at \"\(path)\""
  87. case let .deleteFileFail(path, _):
  88. return "Could not delete file at \"\(path)\""
  89. case let .readFromFileFail(path, _):
  90. return "Could not read from file at \"\(path)\""
  91. case let .writeToFileFail(path, _):
  92. return "Could not write to file at \"\(path)\""
  93. case let .moveFileFail(fromPath, toPath, _):
  94. return "Could not move file at \"\(fromPath)\" to \"\(toPath)\""
  95. case let .copyFileFail(fromPath, toPath, _):
  96. return "Could not copy file from \"\(fromPath)\" to \"\(toPath)\""
  97. case let .attributesChangeFail(path, _):
  98. return "Could not change file attrubutes at \"\(path)\""
  99. }
  100. }
  101. }
  102. // MARK: - CustomStringConvertible
  103. extension FileKitError: CustomStringConvertible {
  104. /// A textual representation of `self`.
  105. public var description: String {
  106. return String(describing: type(of: self)) + "(" + message + ")"
  107. }
  108. }
  109. // MARK: - CustomDebugStringConvertible
  110. extension FileKitError: CustomDebugStringConvertible {
  111. /// A textual representation of this instance, suitable for debugging.
  112. public var debugDescription: String {
  113. if let error = error {
  114. return "\(self.description) \(error)"
  115. }
  116. return self.description
  117. }
  118. }
  119. // MARK: - underlying error
  120. extension FileKitError {
  121. /// Return the underlying error if any
  122. public var error: Error? {
  123. switch self {
  124. case .changeDirectoryFail(_, _, let error),
  125. .createSymlinkFail(_, _, let error),
  126. .createHardlinkFail(_, _, let error),
  127. .createDirectoryFail(_, let error),
  128. .deleteFileFail(_, let error),
  129. .readFromFileFail(_, let error),
  130. .writeToFileFail(_, let error),
  131. .moveFileFail(_, _, let error),
  132. .copyFileFail(_, _, let error):
  133. return error
  134. case .fileDoesNotExist,
  135. .fileAlreadyExists,
  136. .createFileFail,
  137. .attributesChangeFail:
  138. return nil
  139. }
  140. }
  141. }