AFError.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 response contained no data.
  90. /// - `.inputDataNilOrZeroLength`: The response contained no data or the data was zero length.
  91. /// - `.stringSerializationFailed`: String serialization failed using the provided `String.Encoding`.
  92. /// - `.jsonSerializationFailed`: JSON serialization failed with an underlying system error.
  93. /// - `.propertyListSerializationFailed`: Proptery list serialization failed with an underlying system error.
  94. public enum SerializationFailureReason {
  95. case inputDataNil
  96. case inputDataNilOrZeroLength
  97. case stringSerializationFailed(encoding: String.Encoding)
  98. case jsonSerializationFailed(error: Error)
  99. case propertyListSerializationFailed(error: Error)
  100. }
  101. case multipartEncodingFailed(reason: MultipartEncodingFailureReason)
  102. case responseValidationFailed(reason: ValidationFailureReason)
  103. case responseSerializationFailed(reason: SerializationFailureReason)
  104. }
  105. // MARK: - Error Booleans
  106. public extension AFError {
  107. /// Returns whether the AFError is a multipart encoding error. When true, the `url` and `underlyingError` properties
  108. /// will contain the associated values.
  109. public var isMultipartEncodingError: Bool {
  110. if case .multipartEncodingFailed = self { return true }
  111. return false
  112. }
  113. /// Returns whether the `AFError` is a response validation error. When true, the `acceptableContentTypes`,
  114. /// `responseContentType`, and `responseCode` properties will contain the associated values.
  115. public var isResponseValidationError: Bool {
  116. if case .responseValidationFailed = self { return true }
  117. return false
  118. }
  119. /// Returns whether the `AFError` is a response serialization error. When true, the `failedStringEncoding` and
  120. /// `underlyingError` properties will contain the associated values.
  121. public var isResponseSerializationError: Bool {
  122. if case .responseSerializationFailed = self { return true }
  123. return false
  124. }
  125. }
  126. // MARK: - Convenience Properties
  127. public extension AFError {
  128. /// The `URL` associated with the error.
  129. public var url: URL? {
  130. switch self {
  131. case .multipartEncodingFailed(let reason):
  132. return reason.url
  133. default:
  134. return nil
  135. }
  136. }
  137. /// The `Error` returned by a system framework associated with a `.multipartEncodingFailed` or
  138. /// `.responseSerializationFailed` error.
  139. public var underlyingError: Error? {
  140. switch self {
  141. case .multipartEncodingFailed(let reason):
  142. return reason.underlyingError
  143. case .responseSerializationFailed(let reason):
  144. return reason.underlyingError
  145. default:
  146. return nil
  147. }
  148. }
  149. /// The acceptable `Content-Type`s of a `.responseValidationFailed` error.
  150. public var acceptableContentTypes: [String]? {
  151. switch self {
  152. case .responseValidationFailed(let reason):
  153. return reason.acceptableContentTypes
  154. default:
  155. return nil
  156. }
  157. }
  158. /// The response `Content-Type` of a `.responseValidationFailed` error.
  159. public var responseContentType: String? {
  160. switch self {
  161. case .responseValidationFailed(let reason):
  162. return reason.responseContentType
  163. default:
  164. return nil
  165. }
  166. }
  167. /// The response code of a `.responseValidationFailed` error.
  168. public var responseCode: Int? {
  169. switch self {
  170. case .responseValidationFailed(let reason):
  171. return reason.responseCode
  172. default:
  173. return nil
  174. }
  175. }
  176. /// The `String.Encoding` associated with a failed `.stringResponse()` call.
  177. public var failedStringEncoding: String.Encoding? {
  178. switch self {
  179. case .responseSerializationFailed(let reason):
  180. return reason.failedStringEncoding
  181. default:
  182. return nil
  183. }
  184. }
  185. }
  186. extension AFError.MultipartEncodingFailureReason {
  187. var url: URL? {
  188. switch self {
  189. case .bodyPartURLInvalid(let url), .bodyPartFilenameInvalid(let url), .bodyPartFileNotReachable(let url),
  190. .bodyPartFileIsDirectory(let url), .bodyPartFileSizeNotAvailable(let url), .bodyPartInputStreamCreationFailed(let url),
  191. .outputStreamCreationFailed(let url), .outputStreamFileAlreadyExists(let url), .outputStreamURLInvalid(let url),
  192. .bodyPartFileNotReachableWithError(let url, _), .bodyPartFileSizeQueryFailedWithError(let url, _):
  193. return url
  194. default:
  195. return nil
  196. }
  197. }
  198. var underlyingError: Error? {
  199. switch self {
  200. case .bodyPartFileNotReachableWithError(_, let error), .bodyPartFileSizeQueryFailedWithError(_, let error),
  201. .outputStreamWriteFailed(let error), .inputStreamReadFailed(let error):
  202. return error
  203. default:
  204. return nil
  205. }
  206. }
  207. }
  208. extension AFError.ValidationFailureReason {
  209. var acceptableContentTypes: [String]? {
  210. switch self {
  211. case .missingContentType(let types), .unacceptableContentType(let types, _):
  212. return types
  213. default:
  214. return nil
  215. }
  216. }
  217. var responseContentType: String? {
  218. switch self {
  219. case .unacceptableContentType(_, let reponseType):
  220. return reponseType
  221. default:
  222. return nil
  223. }
  224. }
  225. var responseCode: Int? {
  226. switch self {
  227. case .unacceptableStatusCode(let code):
  228. return code
  229. default:
  230. return nil
  231. }
  232. }
  233. }
  234. extension AFError.SerializationFailureReason {
  235. var failedStringEncoding: String.Encoding? {
  236. switch self {
  237. case .stringSerializationFailed(let encoding):
  238. return encoding
  239. default:
  240. return nil
  241. }
  242. }
  243. var underlyingError: Error? {
  244. switch self {
  245. case .jsonSerializationFailed(let error), .propertyListSerializationFailed(let error):
  246. return error
  247. default:
  248. return nil
  249. }
  250. }
  251. }
  252. // MARK: - Error Descriptions
  253. extension AFError: LocalizedError {
  254. public var errorDescription: String? {
  255. switch self {
  256. case .multipartEncodingFailed(let reason):
  257. return reason.localizedDescription
  258. case .responseValidationFailed(let reason):
  259. return reason.localizedDescription
  260. case .responseSerializationFailed(let reason):
  261. return reason.localizedDescription
  262. }
  263. }
  264. }
  265. extension AFError.SerializationFailureReason {
  266. var localizedDescription: String {
  267. switch self {
  268. case .inputDataNil:
  269. return "Response could not be serialized, input data was nil."
  270. case .inputDataNilOrZeroLength:
  271. return "Response could not be serialized, input data was nil or zero length."
  272. case .stringSerializationFailed(let encoding):
  273. return "String could not be serialized with encoding: \(encoding)."
  274. case .jsonSerializationFailed(let error):
  275. return "JSON could not be serialized because of error:\n\(error.localizedDescription)"
  276. case .propertyListSerializationFailed(let error):
  277. return "PropertyList could not be serialized because of error:\n\(error.localizedDescription)"
  278. }
  279. }
  280. }
  281. extension AFError.ValidationFailureReason {
  282. var localizedDescription: String {
  283. switch self {
  284. case .missingContentType(let types):
  285. return "Response Content-Type was missing and acceptable content types (\(types.joined(separator: ","))) do not match \"*/*\"."
  286. case .unacceptableContentType(let acceptableTypes, let responseType):
  287. return "Response Content-Type \"\(responseType)\" does not match any acceptable types: \(acceptableTypes.joined(separator: ","))."
  288. case .unacceptableStatusCode(let code):
  289. return "Response status code was unacceptable: \(code)."
  290. }
  291. }
  292. }
  293. extension AFError.MultipartEncodingFailureReason {
  294. var localizedDescription: String {
  295. switch self {
  296. case .bodyPartURLInvalid(let url):
  297. return "The URL provided is not a file URL: \(url)"
  298. case .bodyPartFilenameInvalid(let url):
  299. return "The URL provided does not have a valid filename: \(url)"
  300. case .bodyPartFileNotReachable(let url):
  301. return "The URL provided is not reachable: \(url)"
  302. case .bodyPartFileNotReachableWithError(let url, let error):
  303. return "The system returned an error while checking the provided URL for reachability.\nURL: \(url)\nError: \(error)"
  304. case .bodyPartFileIsDirectory(let url):
  305. return "The URL provided is a directory: \(url)"
  306. case .bodyPartFileSizeNotAvailable(let url):
  307. return "Could not fetch the file size from the provided URL: \(url)"
  308. case .bodyPartFileSizeQueryFailedWithError(let url, let error):
  309. return "The system returned an error while attempting to fetch the file size from the provided URL.\nURL: \(url)\nError: \(error)"
  310. case .bodyPartInputStreamCreationFailed(let url):
  311. return "Failed to create an InputStream for the provided URL: \(url)"
  312. case .outputStreamCreationFailed(let url):
  313. return "Failed to create an OutputStream for URL: \(url)"
  314. case .outputStreamFileAlreadyExists(let url):
  315. return "A file already exists at the provided URL: \(url)"
  316. case .outputStreamURLInvalid(let url):
  317. return "The provided OutputStream URL is invalid: \(url)"
  318. case .outputStreamWriteFailed(let error):
  319. return "OutputStream write failed with error: \(error)"
  320. case .inputStreamReadFailed(let error):
  321. return "InputStream read failed with error: \(error)"
  322. }
  323. }
  324. }