AFError.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. //
  2. // AFError.swift
  3. //
  4. // Copyright (c) 2014-2018 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 Alamofire. It encompasses a few different types of errors, each with
  26. /// their own associated reasons.
  27. ///
  28. /// - explicitlyCancelled: Returned when a `Request` is explicitly cancelled.
  29. /// - invalidURL: Returned when a `URLConvertible` type fails to create a valid `URL`.
  30. /// - parameterEncodingFailed: Returned when a parameter encoding object throws an error during the encoding process.
  31. /// - parameterEncoderFailed: Returned when a parameter encoder throws an error during the encoding process.
  32. /// - multipartEncodingFailed: Returned when some step in the multipart encoding process fails.
  33. /// - requestAdaptationFailed: Returned when a `RequestAdapter` throws an error during request adaptation.
  34. /// - responseValidationFailed: Returned when a `validate()` call fails.
  35. /// - responseSerializationFailed: Returned when a response serializer throws an error in the serialization process.
  36. /// - serverTrustEvaluationFailed: Returned when a `ServerTrustEvaluating` instance fails during the server trust evaluation process.
  37. /// - requestRetryFailed: Returned when a `RequestRetrier` throws an error during the request retry process.
  38. public enum AFError: Error {
  39. /// The underlying reason the parameter encoding error occurred.
  40. ///
  41. /// - missingURL: The URL request did not have a URL to encode.
  42. /// - jsonEncodingFailed: JSON serialization failed with an underlying system error during the
  43. /// encoding process.
  44. public enum ParameterEncodingFailureReason {
  45. case missingURL
  46. case jsonEncodingFailed(error: Error)
  47. }
  48. /// Underlying reason the parameter encoder error occured.
  49. public enum ParameterEncoderFailureReason {
  50. /// Possible missing components.
  51. public enum RequiredComponent {
  52. /// The `URL` was missing or unable to be extracted from the passed `URLRequest` or during encoding.
  53. case url
  54. /// The `HTTPMethod` could not be extracted from the passed `URLRequest`.
  55. case httpMethod(rawValue: String)
  56. }
  57. /// A `RequiredComponent` was missing during encoding.
  58. case missingRequiredComponent(RequiredComponent)
  59. /// The underlying encoder failed with the associated error.
  60. case encoderFailed(error: Error)
  61. }
  62. /// The underlying reason the multipart encoding error occurred.
  63. ///
  64. /// - bodyPartURLInvalid: The `fileURL` provided for reading an encodable body part isn't a
  65. /// file URL.
  66. /// - bodyPartFilenameInvalid: The filename of the `fileURL` provided has either an empty
  67. /// `lastPathComponent` or `pathExtension.
  68. /// - bodyPartFileNotReachable: The file at the `fileURL` provided was not reachable.
  69. /// - bodyPartFileNotReachableWithError: Attempting to check the reachability of the `fileURL` provided threw
  70. /// an error.
  71. /// - bodyPartFileIsDirectory: The file at the `fileURL` provided is actually a directory.
  72. /// - bodyPartFileSizeNotAvailable: The size of the file at the `fileURL` provided was not returned by
  73. /// the system.
  74. /// - bodyPartFileSizeQueryFailedWithError: The attempt to find the size of the file at the `fileURL` provided
  75. /// threw an error.
  76. /// - bodyPartInputStreamCreationFailed: An `InputStream` could not be created for the provided `fileURL`.
  77. /// - outputStreamCreationFailed: An `OutputStream` could not be created when attempting to write the
  78. /// encoded data to disk.
  79. /// - outputStreamFileAlreadyExists: The encoded body data could not be writtent disk because a file
  80. /// already exists at the provided `fileURL`.
  81. /// - outputStreamURLInvalid: The `fileURL` provided for writing the encoded body data to disk is
  82. /// not a file URL.
  83. /// - outputStreamWriteFailed: The attempt to write the encoded body data to disk failed with an
  84. /// underlying error.
  85. /// - inputStreamReadFailed: The attempt to read an encoded body part `InputStream` failed with
  86. /// underlying system error.
  87. public enum MultipartEncodingFailureReason {
  88. case bodyPartURLInvalid(url: URL)
  89. case bodyPartFilenameInvalid(in: URL)
  90. case bodyPartFileNotReachable(at: URL)
  91. case bodyPartFileNotReachableWithError(atURL: URL, error: Error)
  92. case bodyPartFileIsDirectory(at: URL)
  93. case bodyPartFileSizeNotAvailable(at: URL)
  94. case bodyPartFileSizeQueryFailedWithError(forURL: URL, error: Error)
  95. case bodyPartInputStreamCreationFailed(for: URL)
  96. case outputStreamCreationFailed(for: URL)
  97. case outputStreamFileAlreadyExists(at: URL)
  98. case outputStreamURLInvalid(url: URL)
  99. case outputStreamWriteFailed(error: Error)
  100. case inputStreamReadFailed(error: Error)
  101. }
  102. /// The underlying reason the response validation error occurred.
  103. ///
  104. /// - dataFileNil: The data file containing the server response did not exist.
  105. /// - dataFileReadFailed: The data file containing the server response could not be read.
  106. /// - missingContentType: The response did not contain a `Content-Type` and the `acceptableContentTypes`
  107. /// provided did not contain wildcard type.
  108. /// - unacceptableContentType: The response `Content-Type` did not match any type in the provided
  109. /// `acceptableContentTypes`.
  110. /// - unacceptableStatusCode: The response status code was not acceptable.
  111. public enum ResponseValidationFailureReason {
  112. case dataFileNil
  113. case dataFileReadFailed(at: URL)
  114. case missingContentType(acceptableContentTypes: [String])
  115. case unacceptableContentType(acceptableContentTypes: [String], responseContentType: String)
  116. case unacceptableStatusCode(code: Int)
  117. }
  118. /// The underlying reason the response serialization error occurred.
  119. public enum ResponseSerializationFailureReason {
  120. /// The server response contained no data or the data was zero length.
  121. case inputDataNilOrZeroLength
  122. /// The file containing the server response did not exist.
  123. case inputFileNil
  124. /// The file containing the server response could not be read from the associated `URL`.
  125. case inputFileReadFailed(at: URL)
  126. /// String serialization failed using the provided `String.Encoding`.
  127. case stringSerializationFailed(encoding: String.Encoding)
  128. /// JSON serialization failed with an underlying system error.
  129. case jsonSerializationFailed(error: Error)
  130. /// A `DataDecoder` failed to decode the response due to the associated `Error`.
  131. case decodingFailed(error: Error)
  132. /// Generic serialization failed for an empty response that wasn't type `Empty` but instead the associated type.
  133. case invalidEmptyResponse(type: String)
  134. }
  135. /// Underlying reason a server trust evaluation error occured.
  136. public enum ServerTrustFailureReason {
  137. /// The output of a server trust evaluation.
  138. public struct Output {
  139. /// The host for which the evaluation was performed.
  140. public let host: String
  141. /// The `SecTrust` value which was evaluated.
  142. public let trust: SecTrust
  143. /// The `OSStatus` of evaluation operation.
  144. public let status: OSStatus
  145. /// The result of the evaluation operation.
  146. public let result: SecTrustResultType
  147. /// Creates an `Output` value from the provided values.
  148. init(_ host: String, _ trust: SecTrust, _ status: OSStatus, _ result: SecTrustResultType) {
  149. self.host = host
  150. self.trust = trust
  151. self.status = status
  152. self.result = result
  153. }
  154. }
  155. case noRequiredEvaluator(host: String)
  156. /// No certificates were found with which to perform the trust evaluation.
  157. case noCertificatesFound
  158. /// No public keys were found with which to perform the trust evaluation.
  159. case noPublicKeysFound
  160. /// During evaluation, application of the associated `SecPolicy` failed.
  161. case policyApplicationFailed(trust: SecTrust, policy: SecPolicy, status: OSStatus)
  162. /// During evaluation, setting the associated anchor certificates failed.
  163. case settingAnchorCertificatesFailed(status: OSStatus, certificates: [SecCertificate])
  164. /// During evaluation, creation of the revocation policy failed.
  165. case revocationPolicyCreationFailed
  166. /// Default evaluation failed with the associated `Output`.
  167. case defaultEvaluationFailed(output: Output)
  168. /// Host validation failed with the associated `Output`.
  169. case hostValidationFailed(output: Output)
  170. /// Revocation check failed with the associated `Output` and options.
  171. case revocationCheckFailed(output: Output, options: RevocationTrustEvaluator.Options)
  172. /// Certificate pinning failed.
  173. case certificatePinningFailed(host: String, trust: SecTrust, pinnedCertificates: [SecCertificate], serverCertificates: [SecCertificate])
  174. /// Public key pinning failed.
  175. case publicKeyPinningFailed(host: String, trust: SecTrust, pinnedKeys: [SecKey], serverKeys: [SecKey])
  176. }
  177. case sessionDeinitialized
  178. case sessionInvalidated(error: Error?)
  179. case explicitlyCancelled
  180. case invalidURL(url: URLConvertible)
  181. case parameterEncodingFailed(reason: ParameterEncodingFailureReason)
  182. case parameterEncoderFailed(reason: ParameterEncoderFailureReason)
  183. case multipartEncodingFailed(reason: MultipartEncodingFailureReason)
  184. case requestAdaptationFailed(error: Error)
  185. case responseValidationFailed(reason: ResponseValidationFailureReason)
  186. case responseSerializationFailed(reason: ResponseSerializationFailureReason)
  187. case serverTrustEvaluationFailed(reason: ServerTrustFailureReason)
  188. case requestRetryFailed(retryError: Error, originalError: Error)
  189. }
  190. extension Error {
  191. /// Returns the instance cast as an `AFError`.
  192. public var asAFError: AFError? {
  193. return self as? AFError
  194. }
  195. }
  196. // MARK: - Error Booleans
  197. extension AFError {
  198. // Returns whether the instance is `.sessionDeinitialized`.
  199. public var isSessionDeinitializedError: Bool {
  200. if case .sessionDeinitialized = self { return true }
  201. return false
  202. }
  203. // Returns whether the instance is `.sessionInvalidated`.
  204. public var isSessionInvalidatedError: Bool {
  205. if case .sessionInvalidated = self { return true }
  206. return false
  207. }
  208. /// Returns whether the instance is `.explicitlyCancelled`.
  209. public var isExplicitlyCancelledError: Bool {
  210. if case .explicitlyCancelled = self { return true }
  211. return false
  212. }
  213. /// Returns whether the instance is `.invalidURL`.
  214. public var isInvalidURLError: Bool {
  215. if case .invalidURL = self { return true }
  216. return false
  217. }
  218. /// Returns whether the instance is `.parameterEncodingFailed`. When `true`, the `underlyingError` property will
  219. /// contain the associated value.
  220. public var isParameterEncodingError: Bool {
  221. if case .parameterEncodingFailed = self { return true }
  222. return false
  223. }
  224. /// Returns whether the instance is `.parameterEncoderFailed`. When `true`, the `underlyingError` property will
  225. // contain the associated value.
  226. public var isParameterEncoderError: Bool {
  227. if case .parameterEncoderFailed = self { return true }
  228. return false
  229. }
  230. /// Returns whether the instance is `.multipartEncodingFailed`. When `true`, the `url` and `underlyingError`
  231. /// properties will contain the associated values.
  232. public var isMultipartEncodingError: Bool {
  233. if case .multipartEncodingFailed = self { return true }
  234. return false
  235. }
  236. /// Returns whether the instance is `.requestAdaptationFailed`. When `true`, the `underlyingError` property will
  237. /// contain the associated value.
  238. public var isRequestAdaptationError: Bool {
  239. if case .requestAdaptationFailed = self { return true }
  240. return false
  241. }
  242. /// Returns whether the instance is `.responseValidationFailed`. When `true`, the `acceptableContentTypes`,
  243. /// `responseContentType`, and `responseCode` properties will contain the associated values.
  244. public var isResponseValidationError: Bool {
  245. if case .responseValidationFailed = self { return true }
  246. return false
  247. }
  248. /// Returns whether the instance is `.responseSerializationFailed`. When `true`, the `failedStringEncoding` and
  249. /// `underlyingError` properties will contain the associated values.
  250. public var isResponseSerializationError: Bool {
  251. if case .responseSerializationFailed = self { return true }
  252. return false
  253. }
  254. /// Returns whether the instance is `.serverTrustEvaluationFailed`.
  255. public var isServerTrustEvaluationError: Bool {
  256. if case .serverTrustEvaluationFailed = self { return true }
  257. return false
  258. }
  259. /// Returns whether the instance is `requestRetryFailed`. When `true`, the `underlyingError` property will
  260. /// contain the associated value.
  261. public var isRequestRetryError: Bool {
  262. if case .requestRetryFailed = self { return true }
  263. return false
  264. }
  265. }
  266. // MARK: - Convenience Properties
  267. extension AFError {
  268. /// The `URLConvertible` associated with the error.
  269. public var urlConvertible: URLConvertible? {
  270. guard case .invalidURL(let url) = self else { return nil }
  271. return url
  272. }
  273. /// The `URL` associated with the error.
  274. public var url: URL? {
  275. guard case .multipartEncodingFailed(let reason) = self else { return nil }
  276. return reason.url
  277. }
  278. /// The underlying `Error` responsible for generating the failure associated with `.sessionInvalidated`,
  279. /// `.parameterEncodingFailed`, `.parameterEncoderFailed`, `.multipartEncodingFailed`, `.requestAdaptationFailed`,
  280. /// `.responseSerializationFailed`, `.requestRetryFailed` errors.
  281. public var underlyingError: Error? {
  282. switch self {
  283. case .sessionInvalidated(let error):
  284. return error
  285. case .parameterEncodingFailed(let reason):
  286. return reason.underlyingError
  287. case .parameterEncoderFailed(let reason):
  288. return reason.underlyingError
  289. case .multipartEncodingFailed(let reason):
  290. return reason.underlyingError
  291. case .requestAdaptationFailed(let error):
  292. return error
  293. case .responseSerializationFailed(let reason):
  294. return reason.underlyingError
  295. case .requestRetryFailed(let retryError, _):
  296. return retryError
  297. default:
  298. return nil
  299. }
  300. }
  301. /// The acceptable `Content-Type`s of a `.responseValidationFailed` error.
  302. public var acceptableContentTypes: [String]? {
  303. guard case .responseValidationFailed(let reason) = self else { return nil }
  304. return reason.acceptableContentTypes
  305. }
  306. /// The response `Content-Type` of a `.responseValidationFailed` error.
  307. public var responseContentType: String? {
  308. guard case .responseValidationFailed(let reason) = self else { return nil }
  309. return reason.responseContentType
  310. }
  311. /// The response code of a `.responseValidationFailed` error.
  312. public var responseCode: Int? {
  313. guard case .responseValidationFailed(let reason) = self else { return nil }
  314. return reason.responseCode
  315. }
  316. /// The `String.Encoding` associated with a failed `.stringResponse()` call.
  317. public var failedStringEncoding: String.Encoding? {
  318. guard case .responseSerializationFailed(let reason) = self else { return nil }
  319. return reason.failedStringEncoding
  320. }
  321. }
  322. extension AFError.ParameterEncodingFailureReason {
  323. var underlyingError: Error? {
  324. guard case .jsonEncodingFailed(let error) = self else { return nil }
  325. return error
  326. }
  327. }
  328. extension AFError.ParameterEncoderFailureReason {
  329. var underlyingError: Error? {
  330. guard case .encoderFailed(let error) = self else { return nil }
  331. return error
  332. }
  333. }
  334. extension AFError.MultipartEncodingFailureReason {
  335. var url: URL? {
  336. switch self {
  337. case .bodyPartURLInvalid(let url), .bodyPartFilenameInvalid(let url), .bodyPartFileNotReachable(let url),
  338. .bodyPartFileIsDirectory(let url), .bodyPartFileSizeNotAvailable(let url),
  339. .bodyPartInputStreamCreationFailed(let url), .outputStreamCreationFailed(let url),
  340. .outputStreamFileAlreadyExists(let url), .outputStreamURLInvalid(let url),
  341. .bodyPartFileNotReachableWithError(let url, _), .bodyPartFileSizeQueryFailedWithError(let url, _):
  342. return url
  343. default:
  344. return nil
  345. }
  346. }
  347. var underlyingError: Error? {
  348. switch self {
  349. case .bodyPartFileNotReachableWithError(_, let error), .bodyPartFileSizeQueryFailedWithError(_, let error),
  350. .outputStreamWriteFailed(let error), .inputStreamReadFailed(let error):
  351. return error
  352. default:
  353. return nil
  354. }
  355. }
  356. }
  357. extension AFError.ResponseValidationFailureReason {
  358. var acceptableContentTypes: [String]? {
  359. switch self {
  360. case .missingContentType(let types), .unacceptableContentType(let types, _):
  361. return types
  362. default:
  363. return nil
  364. }
  365. }
  366. var responseContentType: String? {
  367. guard case .unacceptableContentType(_, let responseType) = self else { return nil }
  368. return responseType
  369. }
  370. var responseCode: Int? {
  371. guard case .unacceptableStatusCode(let code) = self else { return nil }
  372. return code
  373. }
  374. }
  375. extension AFError.ResponseSerializationFailureReason {
  376. var failedStringEncoding: String.Encoding? {
  377. guard case .stringSerializationFailed(let encoding) = self else { return nil }
  378. return encoding
  379. }
  380. var underlyingError: Error? {
  381. guard case .jsonSerializationFailed(let error) = self else { return nil }
  382. return error
  383. }
  384. }
  385. extension AFError.ServerTrustFailureReason {
  386. var output: AFError.ServerTrustFailureReason.Output? {
  387. switch self {
  388. case let .defaultEvaluationFailed(output),
  389. let .hostValidationFailed(output),
  390. let .revocationCheckFailed(output, _):
  391. return output
  392. default: return nil
  393. }
  394. }
  395. }
  396. // MARK: - Error Descriptions
  397. extension AFError: LocalizedError {
  398. public var errorDescription: String? {
  399. switch self {
  400. case .sessionDeinitialized:
  401. return """
  402. Session was invalidated without error, so it was likely deinitialized unexpectedly. \
  403. Be sure to retain a reference to your Session for the duration of your requests.
  404. """
  405. case .sessionInvalidated(let error):
  406. return "Session was invalidated with error: \(error?.localizedDescription ?? "No description.")"
  407. case .explicitlyCancelled:
  408. return "Request explicitly cancelled."
  409. case .invalidURL(let url):
  410. return "URL is not valid: \(url)"
  411. case .parameterEncodingFailed(let reason):
  412. return reason.localizedDescription
  413. case .parameterEncoderFailed(let reason):
  414. return reason.localizedDescription
  415. case .multipartEncodingFailed(let reason):
  416. return reason.localizedDescription
  417. case .requestAdaptationFailed(let error):
  418. return "Request adaption failed with error: \(error.localizedDescription)"
  419. case .responseValidationFailed(let reason):
  420. return reason.localizedDescription
  421. case .responseSerializationFailed(let reason):
  422. return reason.localizedDescription
  423. case .serverTrustEvaluationFailed:
  424. return "Server trust evaluation failed."
  425. case .requestRetryFailed(let retryError, let originalError):
  426. return """
  427. Request retry failed with retry error: \(retryError.localizedDescription), \
  428. original error: \(originalError.localizedDescription)
  429. """
  430. }
  431. }
  432. }
  433. extension AFError.ParameterEncodingFailureReason {
  434. var localizedDescription: String {
  435. switch self {
  436. case .missingURL:
  437. return "URL request to encode was missing a URL"
  438. case .jsonEncodingFailed(let error):
  439. return "JSON could not be encoded because of error:\n\(error.localizedDescription)"
  440. }
  441. }
  442. }
  443. extension AFError.ParameterEncoderFailureReason {
  444. var localizedDescription: String {
  445. switch self {
  446. case .missingRequiredComponent(let component):
  447. return "Encoding failed due to a missing request component: \(component)"
  448. case .encoderFailed(let error):
  449. return "The underlying encoder failed with the error: \(error)"
  450. }
  451. }
  452. }
  453. extension AFError.MultipartEncodingFailureReason {
  454. var localizedDescription: String {
  455. switch self {
  456. case .bodyPartURLInvalid(let url):
  457. return "The URL provided is not a file URL: \(url)"
  458. case .bodyPartFilenameInvalid(let url):
  459. return "The URL provided does not have a valid filename: \(url)"
  460. case .bodyPartFileNotReachable(let url):
  461. return "The URL provided is not reachable: \(url)"
  462. case .bodyPartFileNotReachableWithError(let url, let error):
  463. return (
  464. "The system returned an error while checking the provided URL for " +
  465. "reachability.\nURL: \(url)\nError: \(error)"
  466. )
  467. case .bodyPartFileIsDirectory(let url):
  468. return "The URL provided is a directory: \(url)"
  469. case .bodyPartFileSizeNotAvailable(let url):
  470. return "Could not fetch the file size from the provided URL: \(url)"
  471. case .bodyPartFileSizeQueryFailedWithError(let url, let error):
  472. return (
  473. "The system returned an error while attempting to fetch the file size from the " +
  474. "provided URL.\nURL: \(url)\nError: \(error)"
  475. )
  476. case .bodyPartInputStreamCreationFailed(let url):
  477. return "Failed to create an InputStream for the provided URL: \(url)"
  478. case .outputStreamCreationFailed(let url):
  479. return "Failed to create an OutputStream for URL: \(url)"
  480. case .outputStreamFileAlreadyExists(let url):
  481. return "A file already exists at the provided URL: \(url)"
  482. case .outputStreamURLInvalid(let url):
  483. return "The provided OutputStream URL is invalid: \(url)"
  484. case .outputStreamWriteFailed(let error):
  485. return "OutputStream write failed with error: \(error)"
  486. case .inputStreamReadFailed(let error):
  487. return "InputStream read failed with error: \(error)"
  488. }
  489. }
  490. }
  491. extension AFError.ResponseSerializationFailureReason {
  492. var localizedDescription: String {
  493. switch self {
  494. case .inputDataNilOrZeroLength:
  495. return "Response could not be serialized, input data was nil or zero length."
  496. case .inputFileNil:
  497. return "Response could not be serialized, input file was nil."
  498. case .inputFileReadFailed(let url):
  499. return "Response could not be serialized, input file could not be read: \(url)."
  500. case .stringSerializationFailed(let encoding):
  501. return "String could not be serialized with encoding: \(encoding)."
  502. case .jsonSerializationFailed(let error):
  503. return "JSON could not be serialized because of error:\n\(error.localizedDescription)"
  504. case .invalidEmptyResponse(let type):
  505. return "Empty response could not be serialized to type: \(type). Use Empty as the expected type for such responses."
  506. case .decodingFailed(let error):
  507. return "Response could not be decoded because of error:\n\(error.localizedDescription)"
  508. }
  509. }
  510. }
  511. extension AFError.ResponseValidationFailureReason {
  512. var localizedDescription: String {
  513. switch self {
  514. case .dataFileNil:
  515. return "Response could not be validated, data file was nil."
  516. case .dataFileReadFailed(let url):
  517. return "Response could not be validated, data file could not be read: \(url)."
  518. case .missingContentType(let types):
  519. return (
  520. "Response Content-Type was missing and acceptable content types " +
  521. "(\(types.joined(separator: ","))) do not match \"*/*\"."
  522. )
  523. case .unacceptableContentType(let acceptableTypes, let responseType):
  524. return (
  525. "Response Content-Type \"\(responseType)\" does not match any acceptable types: " +
  526. "\(acceptableTypes.joined(separator: ","))."
  527. )
  528. case .unacceptableStatusCode(let code):
  529. return "Response status code was unacceptable: \(code)."
  530. }
  531. }
  532. }
  533. extension AFError.ServerTrustFailureReason {
  534. var localizedDescription: String {
  535. switch self {
  536. case let .noRequiredEvaluator(host):
  537. return "A ServerTrustEvaluating value is required for host \(host) but none was found."
  538. case .noCertificatesFound:
  539. return "No certificates were found or provided for evaluation."
  540. case .noPublicKeysFound:
  541. return "No public keys were found or provided for evaluation."
  542. case .policyApplicationFailed:
  543. return "Attempting to set a SecPolicy failed."
  544. case .settingAnchorCertificatesFailed:
  545. return "Attempting to set the provided certificates as anchor certificates failed."
  546. case .revocationPolicyCreationFailed:
  547. return "Attempting to create a revocation policy failed."
  548. case let .defaultEvaluationFailed(output):
  549. return "Default evaluation failed for host \(output.host)."
  550. case let .hostValidationFailed(output):
  551. return "Host validation failed for host \(output.host)."
  552. case let .revocationCheckFailed(output, _):
  553. return "Revocation check failed for host \(output.host)."
  554. case let .certificatePinningFailed(host, _, _, _):
  555. return "Certificate pinning failed for host \(host)."
  556. case let .publicKeyPinningFailed(host, _, _, _):
  557. return "Public key pinning failed for host \(host)."
  558. }
  559. }
  560. }