AFError.swift 27 KB

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