AFError.swift 26 KB

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