AFError.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // AFError.swift
  3. //
  4. // Copyright (c) 2014-2016 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Foundation
  25. /// `AFError` is the error type returned by the Alamofire framework. It encompasses a few
  26. /// different types of errors, each with their own associated reasons.
  27. ///
  28. /// - `.multipartEncodingFailed` errors are returned when some step in the multipart encoding process fails.
  29. ///
  30. /// - `.responseValidationFailed` errors are returned when a `validate()` call fails.
  31. ///
  32. /// - `.responseSerializationFailed` errors are returned when a response serializer encounters an error in the
  33. /// serialization process.
  34. public enum AFError: Error {
  35. /// The reason underlying the `AFError.multipartEncodingFailed` state.
  36. ///
  37. /// - `.bodyPartURLInvalid`: The `fileURL` provided for reading an encodable body part isn't a
  38. /// file URL.
  39. /// - `.bodyPartFilenameInvalid`: The filename of the `fileURL` provided has either an empty
  40. /// `lastPathComponent` or `pathExtension.
  41. /// - `.bodyPartFileNotReachable`: The file at the `fileURL` provided was not reachable.
  42. /// - `.bodyPartFileNotReachableWithError`: Attempting to check the reachability of the `fileURL` provided threw
  43. /// an error.
  44. /// - `.bodyPartFileIsDirectory`: The file at the `fileURL` provided is actually a directory.
  45. /// - `.bodyPartFileSizeNotAvailable`: The size of the file at the `fileURL` provided was not returned by
  46. /// the system.
  47. /// - `.bodyPartFileSizeQueryFailedWithError`: The attempt to find the size of the file at the `fileURL` provided
  48. /// threw an error.
  49. /// - `.bodyPartInputStreamCreationFailed`: An `InputStream` could not be created for the provided `fileURL`.
  50. /// - `.outputStreamCreationFailed`: An `OutputStream` could not be created when attempting to write the
  51. /// encoded data to disk.
  52. /// - `.outputStreamFileAlreadyExists`: The encoded body data could not be writtent disk because a file already
  53. /// exists at the provided `fileURL`.
  54. /// - `.outputStreamURLInvalid`: The `fileURL` provided for writing the encoded body data to disk is
  55. /// not a file URL.
  56. /// - `.outputStreamWriteFailed`: The attempt to write the encoded body data to disk failed with an
  57. /// underlying error.
  58. /// - `.inputStreamReadFailed`: The attempt to read an encoded body part `InputStream` failed with
  59. /// underlying system error.
  60. public enum MultipartEncodingFailureReason {
  61. case bodyPartURLInvalid(url: URL)
  62. case bodyPartFilenameInvalid(in: URL)
  63. case bodyPartFileNotReachable(at: URL)
  64. case bodyPartFileNotReachableWithError(atURL: URL, error: Error)
  65. case bodyPartFileIsDirectory(at: URL)
  66. case bodyPartFileSizeNotAvailable(at: URL)
  67. case bodyPartFileSizeQueryFailedWithError(forURL: URL, error: Error)
  68. case bodyPartInputStreamCreationFailed(for: URL)
  69. case outputStreamCreationFailed(for: URL)
  70. case outputStreamFileAlreadyExists(at: URL)
  71. case outputStreamURLInvalid(url: URL)
  72. case outputStreamWriteFailed(error: Error)
  73. case inputStreamReadFailed(error: Error)
  74. }
  75. /// The reason underlying the `AFError.responseValidationFailed` state.
  76. ///
  77. /// - `.missingContentType`: The response did not contain a `Content-Type` and the `acceptableContentTypes`
  78. /// provided did not contain wildcard type.
  79. /// - `unacceptableContentType`: The response `Content-Type` did not match any type in the provided
  80. /// . `acceptableContentTypes`.
  81. /// - `.unacceptableStatusCode`: The response status code was not acceptable.
  82. public enum ValidationFailureReason {
  83. case missingContentType(acceptableContentTypes: [String])
  84. case unacceptableContentType(acceptableContentTypes: [String], responseContentType: String)
  85. case unacceptableStatusCode(code: Int)
  86. }
  87. /// The reason underlying the `AFError.responseSerializationFailed` state.
  88. ///
  89. /// - `.inputDataNil`: The server response contained no data.
  90. /// - `.inputDataNilOrZeroLength`: The server response contained no data or the data was zero length.
  91. /// - `.inputFileNil`: The file containing the server response did not exist.
  92. /// - `.inputFileReadFailed`: The file containing the server response could not be read.
  93. /// - `.stringSerializationFailed`: String serialization failed using the provided `String.Encoding`.
  94. /// - `.jsonSerializationFailed`: JSON serialization failed with an underlying system error.
  95. /// - `.propertyListSerializationFailed`: Proptery list serialization failed with an underlying system error.
  96. public enum SerializationFailureReason {
  97. case inputDataNil
  98. case inputDataNilOrZeroLength
  99. case inputFileNil
  100. case inputFileReadFailed(at: URL)
  101. case stringSerializationFailed(encoding: String.Encoding)
  102. case jsonSerializationFailed(error: Error)
  103. case propertyListSerializationFailed(error: Error)
  104. }
  105. case multipartEncodingFailed(reason: MultipartEncodingFailureReason)
  106. case responseValidationFailed(reason: ValidationFailureReason)
  107. case responseSerializationFailed(reason: SerializationFailureReason)
  108. }
  109. // MARK: - Error Booleans
  110. public extension AFError {
  111. /// Returns whether the AFError is a multipart encoding error. When true, the `url` and `underlyingError` properties
  112. /// will contain the associated values.
  113. public var isMultipartEncodingError: Bool {
  114. if case .multipartEncodingFailed = self { return true }
  115. return false
  116. }
  117. /// Returns whether the `AFError` is a response validation error. When true, the `acceptableContentTypes`,
  118. /// `responseContentType`, and `responseCode` properties will contain the associated values.
  119. public var isResponseValidationError: Bool {
  120. if case .responseValidationFailed = self { return true }
  121. return false
  122. }
  123. /// Returns whether the `AFError` is a response serialization error. When true, the `failedStringEncoding` and
  124. /// `underlyingError` properties will contain the associated values.
  125. public var isResponseSerializationError: Bool {
  126. if case .responseSerializationFailed = self { return true }
  127. return false
  128. }
  129. }
  130. // MARK: - Convenience Properties
  131. public extension AFError {
  132. /// The `URL` associated with the error.
  133. public var url: URL? {
  134. switch self {
  135. case .multipartEncodingFailed(let reason):
  136. return reason.url
  137. default:
  138. return nil
  139. }
  140. }
  141. /// The `Error` returned by a system framework associated with a `.multipartEncodingFailed` or
  142. /// `.responseSerializationFailed` error.
  143. public var underlyingError: Error? {
  144. switch self {
  145. case .multipartEncodingFailed(let reason):
  146. return reason.underlyingError
  147. case .responseSerializationFailed(let reason):
  148. return reason.underlyingError
  149. default:
  150. return nil
  151. }
  152. }
  153. /// The acceptable `Content-Type`s of a `.responseValidationFailed` error.
  154. public var acceptableContentTypes: [String]? {
  155. switch self {
  156. case .responseValidationFailed(let reason):
  157. return reason.acceptableContentTypes
  158. default:
  159. return nil
  160. }
  161. }
  162. /// The response `Content-Type` of a `.responseValidationFailed` error.
  163. public var responseContentType: String? {
  164. switch self {
  165. case .responseValidationFailed(let reason):
  166. return reason.responseContentType
  167. default:
  168. return nil
  169. }
  170. }
  171. /// The response code of a `.responseValidationFailed` error.
  172. public var responseCode: Int? {
  173. switch self {
  174. case .responseValidationFailed(let reason):
  175. return reason.responseCode
  176. default:
  177. return nil
  178. }
  179. }
  180. /// The `String.Encoding` associated with a failed `.stringResponse()` call.
  181. public var failedStringEncoding: String.Encoding? {
  182. switch self {
  183. case .responseSerializationFailed(let reason):
  184. return reason.failedStringEncoding
  185. default:
  186. return nil
  187. }
  188. }
  189. }
  190. extension AFError.MultipartEncodingFailureReason {
  191. var url: URL? {
  192. switch self {
  193. case .bodyPartURLInvalid(let url), .bodyPartFilenameInvalid(let url), .bodyPartFileNotReachable(let url),
  194. .bodyPartFileIsDirectory(let url), .bodyPartFileSizeNotAvailable(let url), .bodyPartInputStreamCreationFailed(let url),
  195. .outputStreamCreationFailed(let url), .outputStreamFileAlreadyExists(let url), .outputStreamURLInvalid(let url),
  196. .bodyPartFileNotReachableWithError(let url, _), .bodyPartFileSizeQueryFailedWithError(let url, _):
  197. return url
  198. default:
  199. return nil
  200. }
  201. }
  202. var underlyingError: Error? {
  203. switch self {
  204. case .bodyPartFileNotReachableWithError(_, let error), .bodyPartFileSizeQueryFailedWithError(_, let error),
  205. .outputStreamWriteFailed(let error), .inputStreamReadFailed(let error):
  206. return error
  207. default:
  208. return nil
  209. }
  210. }
  211. }
  212. extension AFError.ValidationFailureReason {
  213. var acceptableContentTypes: [String]? {
  214. switch self {
  215. case .missingContentType(let types), .unacceptableContentType(let types, _):
  216. return types
  217. default:
  218. return nil
  219. }
  220. }
  221. var responseContentType: String? {
  222. switch self {
  223. case .unacceptableContentType(_, let reponseType):
  224. return reponseType
  225. default:
  226. return nil
  227. }
  228. }
  229. var responseCode: Int? {
  230. switch self {
  231. case .unacceptableStatusCode(let code):
  232. return code
  233. default:
  234. return nil
  235. }
  236. }
  237. }
  238. extension AFError.SerializationFailureReason {
  239. var failedStringEncoding: String.Encoding? {
  240. switch self {
  241. case .stringSerializationFailed(let encoding):
  242. return encoding
  243. default:
  244. return nil
  245. }
  246. }
  247. var underlyingError: Error? {
  248. switch self {
  249. case .jsonSerializationFailed(let error), .propertyListSerializationFailed(let error):
  250. return error
  251. default:
  252. return nil
  253. }
  254. }
  255. }
  256. // MARK: - Error Descriptions
  257. extension AFError: LocalizedError {
  258. public var errorDescription: String? {
  259. switch self {
  260. case .multipartEncodingFailed(let reason):
  261. return reason.localizedDescription
  262. case .responseValidationFailed(let reason):
  263. return reason.localizedDescription
  264. case .responseSerializationFailed(let reason):
  265. return reason.localizedDescription
  266. }
  267. }
  268. }
  269. extension AFError.SerializationFailureReason {
  270. var localizedDescription: String {
  271. switch self {
  272. case .inputDataNil:
  273. return "Response could not be serialized, input data was nil."
  274. case .inputDataNilOrZeroLength:
  275. return "Response could not be serialized, input data was nil or zero length."
  276. case .inputFileNil:
  277. return "Response could not be serialized, input file was nil."
  278. case .inputFileReadFailed(let url):
  279. return "Response could not be serialized, input file could not be read: \(url)."
  280. case .stringSerializationFailed(let encoding):
  281. return "String could not be serialized with encoding: \(encoding)."
  282. case .jsonSerializationFailed(let error):
  283. return "JSON could not be serialized because of error:\n\(error.localizedDescription)"
  284. case .propertyListSerializationFailed(let error):
  285. return "PropertyList could not be serialized because of error:\n\(error.localizedDescription)"
  286. }
  287. }
  288. }
  289. extension AFError.ValidationFailureReason {
  290. var localizedDescription: String {
  291. switch self {
  292. case .missingContentType(let types):
  293. return "Response Content-Type was missing and acceptable content types (\(types.joined(separator: ","))) do not match \"*/*\"."
  294. case .unacceptableContentType(let acceptableTypes, let responseType):
  295. return "Response Content-Type \"\(responseType)\" does not match any acceptable types: \(acceptableTypes.joined(separator: ","))."
  296. case .unacceptableStatusCode(let code):
  297. return "Response status code was unacceptable: \(code)."
  298. }
  299. }
  300. }
  301. extension AFError.MultipartEncodingFailureReason {
  302. var localizedDescription: String {
  303. switch self {
  304. case .bodyPartURLInvalid(let url):
  305. return "The URL provided is not a file URL: \(url)"
  306. case .bodyPartFilenameInvalid(let url):
  307. return "The URL provided does not have a valid filename: \(url)"
  308. case .bodyPartFileNotReachable(let url):
  309. return "The URL provided is not reachable: \(url)"
  310. case .bodyPartFileNotReachableWithError(let url, let error):
  311. return "The system returned an error while checking the provided URL for reachability.\nURL: \(url)\nError: \(error)"
  312. case .bodyPartFileIsDirectory(let url):
  313. return "The URL provided is a directory: \(url)"
  314. case .bodyPartFileSizeNotAvailable(let url):
  315. return "Could not fetch the file size from the provided URL: \(url)"
  316. case .bodyPartFileSizeQueryFailedWithError(let url, let error):
  317. return "The system returned an error while attempting to fetch the file size from the provided URL.\nURL: \(url)\nError: \(error)"
  318. case .bodyPartInputStreamCreationFailed(let url):
  319. return "Failed to create an InputStream for the provided URL: \(url)"
  320. case .outputStreamCreationFailed(let url):
  321. return "Failed to create an OutputStream for URL: \(url)"
  322. case .outputStreamFileAlreadyExists(let url):
  323. return "A file already exists at the provided URL: \(url)"
  324. case .outputStreamURLInvalid(let url):
  325. return "The provided OutputStream URL is invalid: \(url)"
  326. case .outputStreamWriteFailed(let error):
  327. return "OutputStream write failed with error: \(error)"
  328. case .inputStreamReadFailed(let error):
  329. return "InputStream read failed with error: \(error)"
  330. }
  331. }
  332. }