AFError.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. /// A response serializer was added to the request after the request was already finished.
  135. case responseSerializerAddedAfterRequestFinished
  136. }
  137. /// Underlying reason a server trust evaluation error occured.
  138. public enum ServerTrustFailureReason {
  139. /// The output of a server trust evaluation.
  140. public struct Output {
  141. /// The host for which the evaluation was performed.
  142. public let host: String
  143. /// The `SecTrust` value which was evaluated.
  144. public let trust: SecTrust
  145. /// The `OSStatus` of evaluation operation.
  146. public let status: OSStatus
  147. /// The result of the evaluation operation.
  148. public let result: SecTrustResultType
  149. /// Creates an `Output` value from the provided values.
  150. init(_ host: String, _ trust: SecTrust, _ status: OSStatus, _ result: SecTrustResultType) {
  151. self.host = host
  152. self.trust = trust
  153. self.status = status
  154. self.result = result
  155. }
  156. }
  157. case noRequiredEvaluator(host: String)
  158. /// No certificates were found with which to perform the trust evaluation.
  159. case noCertificatesFound
  160. /// No public keys were found with which to perform the trust evaluation.
  161. case noPublicKeysFound
  162. /// During evaluation, application of the associated `SecPolicy` failed.
  163. case policyApplicationFailed(trust: SecTrust, policy: SecPolicy, status: OSStatus)
  164. /// During evaluation, setting the associated anchor certificates failed.
  165. case settingAnchorCertificatesFailed(status: OSStatus, certificates: [SecCertificate])
  166. /// During evaluation, creation of the revocation policy failed.
  167. case revocationPolicyCreationFailed
  168. /// Default evaluation failed with the associated `Output`.
  169. case defaultEvaluationFailed(output: Output)
  170. /// Host validation failed with the associated `Output`.
  171. case hostValidationFailed(output: Output)
  172. /// Revocation check failed with the associated `Output` and options.
  173. case revocationCheckFailed(output: Output, options: RevocationTrustEvaluator.Options)
  174. /// Certificate pinning failed.
  175. case certificatePinningFailed(host: String, trust: SecTrust, pinnedCertificates: [SecCertificate], serverCertificates: [SecCertificate])
  176. /// Public key pinning failed.
  177. case publicKeyPinningFailed(host: String, trust: SecTrust, pinnedKeys: [SecKey], serverKeys: [SecKey])
  178. }
  179. case sessionDeinitialized
  180. case sessionInvalidated(error: Error?)
  181. case explicitlyCancelled
  182. case invalidURL(url: URLConvertible)
  183. case parameterEncodingFailed(reason: ParameterEncodingFailureReason)
  184. case parameterEncoderFailed(reason: ParameterEncoderFailureReason)
  185. case multipartEncodingFailed(reason: MultipartEncodingFailureReason)
  186. case requestAdaptationFailed(error: Error)
  187. case responseValidationFailed(reason: ResponseValidationFailureReason)
  188. case responseSerializationFailed(reason: ResponseSerializationFailureReason)
  189. case serverTrustEvaluationFailed(reason: ServerTrustFailureReason)
  190. case requestRetryFailed(retryError: Error, originalError: Error)
  191. }
  192. extension Error {
  193. /// Returns the instance cast as an `AFError`.
  194. public var asAFError: AFError? {
  195. return self as? AFError
  196. }
  197. }
  198. // MARK: - Error Booleans
  199. extension AFError {
  200. // Returns whether the instance is `.sessionDeinitialized`.
  201. public var isSessionDeinitializedError: Bool {
  202. if case .sessionDeinitialized = self { return true }
  203. return false
  204. }
  205. // Returns whether the instance is `.sessionInvalidated`.
  206. public var isSessionInvalidatedError: Bool {
  207. if case .sessionInvalidated = self { return true }
  208. return false
  209. }
  210. /// Returns whether the instance is `.explicitlyCancelled`.
  211. public var isExplicitlyCancelledError: Bool {
  212. if case .explicitlyCancelled = self { return true }
  213. return false
  214. }
  215. /// Returns whether the instance is `.invalidURL`.
  216. public var isInvalidURLError: Bool {
  217. if case .invalidURL = self { return true }
  218. return false
  219. }
  220. /// Returns whether the instance is `.parameterEncodingFailed`. When `true`, the `underlyingError` property will
  221. /// contain the associated value.
  222. public var isParameterEncodingError: Bool {
  223. if case .parameterEncodingFailed = self { return true }
  224. return false
  225. }
  226. /// Returns whether the instance is `.parameterEncoderFailed`. When `true`, the `underlyingError` property will
  227. // contain the associated value.
  228. public var isParameterEncoderError: Bool {
  229. if case .parameterEncoderFailed = self { return true }
  230. return false
  231. }
  232. /// Returns whether the instance is `.multipartEncodingFailed`. When `true`, the `url` and `underlyingError`
  233. /// properties will contain the associated values.
  234. public var isMultipartEncodingError: Bool {
  235. if case .multipartEncodingFailed = self { return true }
  236. return false
  237. }
  238. /// Returns whether the instance is `.requestAdaptationFailed`. When `true`, the `underlyingError` property will
  239. /// contain the associated value.
  240. public var isRequestAdaptationError: Bool {
  241. if case .requestAdaptationFailed = self { return true }
  242. return false
  243. }
  244. /// Returns whether the instance is `.responseValidationFailed`. When `true`, the `acceptableContentTypes`,
  245. /// `responseContentType`, and `responseCode` properties will contain the associated values.
  246. public var isResponseValidationError: Bool {
  247. if case .responseValidationFailed = self { return true }
  248. return false
  249. }
  250. /// Returns whether the instance is `.responseSerializationFailed`. When `true`, the `failedStringEncoding` and
  251. /// `underlyingError` properties will contain the associated values.
  252. public var isResponseSerializationError: Bool {
  253. if case .responseSerializationFailed = self { return true }
  254. return false
  255. }
  256. /// Returns whether the instance is `.serverTrustEvaluationFailed`.
  257. public var isServerTrustEvaluationError: Bool {
  258. if case .serverTrustEvaluationFailed = self { return true }
  259. return false
  260. }
  261. /// Returns whether the instance is `requestRetryFailed`. When `true`, the `underlyingError` property will
  262. /// contain the associated value.
  263. public var isRequestRetryError: Bool {
  264. if case .requestRetryFailed = self { return true }
  265. return false
  266. }
  267. }
  268. // MARK: - Convenience Properties
  269. extension AFError {
  270. /// The `URLConvertible` associated with the error.
  271. public var urlConvertible: URLConvertible? {
  272. guard case .invalidURL(let url) = self else { return nil }
  273. return url
  274. }
  275. /// The `URL` associated with the error.
  276. public var url: URL? {
  277. guard case .multipartEncodingFailed(let reason) = self else { return nil }
  278. return reason.url
  279. }
  280. /// The underlying `Error` responsible for generating the failure associated with `.sessionInvalidated`,
  281. /// `.parameterEncodingFailed`, `.parameterEncoderFailed`, `.multipartEncodingFailed`, `.requestAdaptationFailed`,
  282. /// `.responseSerializationFailed`, `.requestRetryFailed` errors.
  283. public var underlyingError: Error? {
  284. switch self {
  285. case .sessionInvalidated(let error):
  286. return error
  287. case .parameterEncodingFailed(let reason):
  288. return reason.underlyingError
  289. case .parameterEncoderFailed(let reason):
  290. return reason.underlyingError
  291. case .multipartEncodingFailed(let reason):
  292. return reason.underlyingError
  293. case .requestAdaptationFailed(let error):
  294. return error
  295. case .responseSerializationFailed(let reason):
  296. return reason.underlyingError
  297. case .requestRetryFailed(let retryError, _):
  298. return retryError
  299. default:
  300. return nil
  301. }
  302. }
  303. /// The acceptable `Content-Type`s of a `.responseValidationFailed` error.
  304. public var acceptableContentTypes: [String]? {
  305. guard case .responseValidationFailed(let reason) = self else { return nil }
  306. return reason.acceptableContentTypes
  307. }
  308. /// The response `Content-Type` of a `.responseValidationFailed` error.
  309. public var responseContentType: String? {
  310. guard case .responseValidationFailed(let reason) = self else { return nil }
  311. return reason.responseContentType
  312. }
  313. /// The response code of a `.responseValidationFailed` error.
  314. public var responseCode: Int? {
  315. guard case .responseValidationFailed(let reason) = self else { return nil }
  316. return reason.responseCode
  317. }
  318. /// The `String.Encoding` associated with a failed `.stringResponse()` call.
  319. public var failedStringEncoding: String.Encoding? {
  320. guard case .responseSerializationFailed(let reason) = self else { return nil }
  321. return reason.failedStringEncoding
  322. }
  323. }
  324. extension AFError.ParameterEncodingFailureReason {
  325. var underlyingError: Error? {
  326. guard case .jsonEncodingFailed(let error) = self else { return nil }
  327. return error
  328. }
  329. }
  330. extension AFError.ParameterEncoderFailureReason {
  331. var underlyingError: Error? {
  332. guard case .encoderFailed(let error) = self else { return nil }
  333. return error
  334. }
  335. }
  336. extension AFError.MultipartEncodingFailureReason {
  337. var url: URL? {
  338. switch self {
  339. case .bodyPartURLInvalid(let url), .bodyPartFilenameInvalid(let url), .bodyPartFileNotReachable(let url),
  340. .bodyPartFileIsDirectory(let url), .bodyPartFileSizeNotAvailable(let url),
  341. .bodyPartInputStreamCreationFailed(let url), .outputStreamCreationFailed(let url),
  342. .outputStreamFileAlreadyExists(let url), .outputStreamURLInvalid(let url),
  343. .bodyPartFileNotReachableWithError(let url, _), .bodyPartFileSizeQueryFailedWithError(let url, _):
  344. return url
  345. default:
  346. return nil
  347. }
  348. }
  349. var underlyingError: Error? {
  350. switch self {
  351. case .bodyPartFileNotReachableWithError(_, let error), .bodyPartFileSizeQueryFailedWithError(_, let error),
  352. .outputStreamWriteFailed(let error), .inputStreamReadFailed(let error):
  353. return error
  354. default:
  355. return nil
  356. }
  357. }
  358. }
  359. extension AFError.ResponseValidationFailureReason {
  360. var acceptableContentTypes: [String]? {
  361. switch self {
  362. case .missingContentType(let types), .unacceptableContentType(let types, _):
  363. return types
  364. default:
  365. return nil
  366. }
  367. }
  368. var responseContentType: String? {
  369. guard case .unacceptableContentType(_, let responseType) = self else { return nil }
  370. return responseType
  371. }
  372. var responseCode: Int? {
  373. guard case .unacceptableStatusCode(let code) = self else { return nil }
  374. return code
  375. }
  376. }
  377. extension AFError.ResponseSerializationFailureReason {
  378. var failedStringEncoding: String.Encoding? {
  379. guard case .stringSerializationFailed(let encoding) = self else { return nil }
  380. return encoding
  381. }
  382. var underlyingError: Error? {
  383. guard case .jsonSerializationFailed(let error) = self else { return nil }
  384. return error
  385. }
  386. }
  387. extension AFError.ServerTrustFailureReason {
  388. var output: AFError.ServerTrustFailureReason.Output? {
  389. switch self {
  390. case let .defaultEvaluationFailed(output),
  391. let .hostValidationFailed(output),
  392. let .revocationCheckFailed(output, _):
  393. return output
  394. default: return nil
  395. }
  396. }
  397. }
  398. // MARK: - Error Descriptions
  399. extension AFError: LocalizedError {
  400. public var errorDescription: String? {
  401. switch self {
  402. case .sessionDeinitialized:
  403. return """
  404. Session was invalidated without error, so it was likely deinitialized unexpectedly. \
  405. Be sure to retain a reference to your Session for the duration of your requests.
  406. """
  407. case .sessionInvalidated(let error):
  408. return "Session was invalidated with error: \(error?.localizedDescription ?? "No description.")"
  409. case .explicitlyCancelled:
  410. return "Request explicitly cancelled."
  411. case .invalidURL(let url):
  412. return "URL is not valid: \(url)"
  413. case .parameterEncodingFailed(let reason):
  414. return reason.localizedDescription
  415. case .parameterEncoderFailed(let reason):
  416. return reason.localizedDescription
  417. case .multipartEncodingFailed(let reason):
  418. return reason.localizedDescription
  419. case .requestAdaptationFailed(let error):
  420. return "Request adaption failed with error: \(error.localizedDescription)"
  421. case .responseValidationFailed(let reason):
  422. return reason.localizedDescription
  423. case .responseSerializationFailed(let reason):
  424. return reason.localizedDescription
  425. case .serverTrustEvaluationFailed:
  426. return "Server trust evaluation failed."
  427. case .requestRetryFailed(let retryError, let originalError):
  428. return """
  429. Request retry failed with retry error: \(retryError.localizedDescription), \
  430. original error: \(originalError.localizedDescription)
  431. """
  432. }
  433. }
  434. }
  435. extension AFError.ParameterEncodingFailureReason {
  436. var localizedDescription: String {
  437. switch self {
  438. case .missingURL:
  439. return "URL request to encode was missing a URL"
  440. case .jsonEncodingFailed(let error):
  441. return "JSON could not be encoded because of error:\n\(error.localizedDescription)"
  442. }
  443. }
  444. }
  445. extension AFError.ParameterEncoderFailureReason {
  446. var localizedDescription: String {
  447. switch self {
  448. case .missingRequiredComponent(let component):
  449. return "Encoding failed due to a missing request component: \(component)"
  450. case .encoderFailed(let error):
  451. return "The underlying encoder failed with the error: \(error)"
  452. }
  453. }
  454. }
  455. extension AFError.MultipartEncodingFailureReason {
  456. var localizedDescription: String {
  457. switch self {
  458. case .bodyPartURLInvalid(let url):
  459. return "The URL provided is not a file URL: \(url)"
  460. case .bodyPartFilenameInvalid(let url):
  461. return "The URL provided does not have a valid filename: \(url)"
  462. case .bodyPartFileNotReachable(let url):
  463. return "The URL provided is not reachable: \(url)"
  464. case .bodyPartFileNotReachableWithError(let url, let error):
  465. return (
  466. "The system returned an error while checking the provided URL for " +
  467. "reachability.\nURL: \(url)\nError: \(error)"
  468. )
  469. case .bodyPartFileIsDirectory(let url):
  470. return "The URL provided is a directory: \(url)"
  471. case .bodyPartFileSizeNotAvailable(let url):
  472. return "Could not fetch the file size from the provided URL: \(url)"
  473. case .bodyPartFileSizeQueryFailedWithError(let url, let error):
  474. return (
  475. "The system returned an error while attempting to fetch the file size from the " +
  476. "provided URL.\nURL: \(url)\nError: \(error)"
  477. )
  478. case .bodyPartInputStreamCreationFailed(let url):
  479. return "Failed to create an InputStream for the provided URL: \(url)"
  480. case .outputStreamCreationFailed(let url):
  481. return "Failed to create an OutputStream for URL: \(url)"
  482. case .outputStreamFileAlreadyExists(let url):
  483. return "A file already exists at the provided URL: \(url)"
  484. case .outputStreamURLInvalid(let url):
  485. return "The provided OutputStream URL is invalid: \(url)"
  486. case .outputStreamWriteFailed(let error):
  487. return "OutputStream write failed with error: \(error)"
  488. case .inputStreamReadFailed(let error):
  489. return "InputStream read failed with error: \(error)"
  490. }
  491. }
  492. }
  493. extension AFError.ResponseSerializationFailureReason {
  494. var localizedDescription: String {
  495. switch self {
  496. case .inputDataNilOrZeroLength:
  497. return "Response could not be serialized, input data was nil or zero length."
  498. case .inputFileNil:
  499. return "Response could not be serialized, input file was nil."
  500. case .inputFileReadFailed(let url):
  501. return "Response could not be serialized, input file could not be read: \(url)."
  502. case .stringSerializationFailed(let encoding):
  503. return "String could not be serialized with encoding: \(encoding)."
  504. case .jsonSerializationFailed(let error):
  505. return "JSON could not be serialized because of error:\n\(error.localizedDescription)"
  506. case .invalidEmptyResponse(let type):
  507. return "Empty response could not be serialized to type: \(type). Use Empty as the expected type for such responses."
  508. case .decodingFailed(let error):
  509. return "Response could not be decoded because of error:\n\(error.localizedDescription)"
  510. case .responseSerializerAddedAfterRequestFinished:
  511. return "Response serializer was added to the request after it had already finished."
  512. }
  513. }
  514. }
  515. extension AFError.ResponseValidationFailureReason {
  516. var localizedDescription: String {
  517. switch self {
  518. case .dataFileNil:
  519. return "Response could not be validated, data file was nil."
  520. case .dataFileReadFailed(let url):
  521. return "Response could not be validated, data file could not be read: \(url)."
  522. case .missingContentType(let types):
  523. return (
  524. "Response Content-Type was missing and acceptable content types " +
  525. "(\(types.joined(separator: ","))) do not match \"*/*\"."
  526. )
  527. case .unacceptableContentType(let acceptableTypes, let responseType):
  528. return (
  529. "Response Content-Type \"\(responseType)\" does not match any acceptable types: " +
  530. "\(acceptableTypes.joined(separator: ","))."
  531. )
  532. case .unacceptableStatusCode(let code):
  533. return "Response status code was unacceptable: \(code)."
  534. }
  535. }
  536. }
  537. extension AFError.ServerTrustFailureReason {
  538. var localizedDescription: String {
  539. switch self {
  540. case let .noRequiredEvaluator(host):
  541. return "A ServerTrustEvaluating value is required for host \(host) but none was found."
  542. case .noCertificatesFound:
  543. return "No certificates were found or provided for evaluation."
  544. case .noPublicKeysFound:
  545. return "No public keys were found or provided for evaluation."
  546. case .policyApplicationFailed:
  547. return "Attempting to set a SecPolicy failed."
  548. case .settingAnchorCertificatesFailed:
  549. return "Attempting to set the provided certificates as anchor certificates failed."
  550. case .revocationPolicyCreationFailed:
  551. return "Attempting to create a revocation policy failed."
  552. case let .defaultEvaluationFailed(output):
  553. return "Default evaluation failed for host \(output.host)."
  554. case let .hostValidationFailed(output):
  555. return "Host validation failed for host \(output.host)."
  556. case let .revocationCheckFailed(output, _):
  557. return "Revocation check failed for host \(output.host)."
  558. case let .certificatePinningFailed(host, _, _, _):
  559. return "Certificate pinning failed for host \(host)."
  560. case let .publicKeyPinningFailed(host, _, _, _):
  561. return "Public key pinning failed for host \(host)."
  562. }
  563. }
  564. }