AFError.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 `.multipartEncodingFailed` error occurred.
  29. public enum MultipartEncodingFailureReason {
  30. /// The `fileURL` provided for reading an encodable body part isn't a file `URL`.
  31. case bodyPartURLInvalid(url: URL)
  32. /// The filename of the `fileURL` provided has either an empty `lastPathComponent` or `pathExtension.
  33. case bodyPartFilenameInvalid(in: URL)
  34. /// The file at the `fileURL` provided was not reachable.
  35. case bodyPartFileNotReachable(at: URL)
  36. /// Attempting to check the reachability of the `fileURL` provided threw an error.
  37. case bodyPartFileNotReachableWithError(atURL: URL, error: Error)
  38. /// The file at the `fileURL` provided is actually a directory.
  39. case bodyPartFileIsDirectory(at: URL)
  40. /// The size of the file at the `fileURL` provided was not returned by the system.
  41. case bodyPartFileSizeNotAvailable(at: URL)
  42. /// The attempt to find the size of the file at the `fileURL` provided threw an error.
  43. case bodyPartFileSizeQueryFailedWithError(forURL: URL, error: Error)
  44. /// An `InputStream` could not be created for the provided `fileURL`.
  45. case bodyPartInputStreamCreationFailed(for: URL)
  46. /// An `OutputStream` could not be created when attempting to write the encoded data to disk.
  47. case outputStreamCreationFailed(for: URL)
  48. /// The encoded body data could not be written to disk because a file already exists at the provided `fileURL`.
  49. case outputStreamFileAlreadyExists(at: URL)
  50. /// The `fileURL` provided for writing the encoded body data to disk is not a file `URL`.
  51. case outputStreamURLInvalid(url: URL)
  52. /// The attempt to write the encoded body data to disk failed with an underlying error.
  53. case outputStreamWriteFailed(error: Error)
  54. /// The attempt to read an encoded body part `InputStream` failed with underlying system error.
  55. case inputStreamReadFailed(error: Error)
  56. }
  57. /// The underlying reason the `.parameterEncodingFailed` error occurred.
  58. public enum ParameterEncodingFailureReason {
  59. /// The `URLRequest` did not have a `URL` to encode.
  60. case missingURL
  61. /// JSON serialization failed with an underlying system error during the encoding process.
  62. case jsonEncodingFailed(error: Error)
  63. }
  64. /// The underlying reason the `.parameterEncoderFailed` error occurred.
  65. public enum ParameterEncoderFailureReason {
  66. /// Possible missing components.
  67. public enum RequiredComponent {
  68. /// The `URL` was missing or unable to be extracted from the passed `URLRequest` or during encoding.
  69. case url
  70. /// The `HTTPMethod` could not be extracted from the passed `URLRequest`.
  71. case httpMethod(rawValue: String)
  72. }
  73. /// A `RequiredComponent` was missing during encoding.
  74. case missingRequiredComponent(RequiredComponent)
  75. /// The underlying encoder failed with the associated error.
  76. case encoderFailed(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. /// The underlying reason the `.urlRequestValidationFailed`
  153. public enum URLRequestValidationFailureReason {
  154. case bodyDataInGETRequest(Data)
  155. }
  156. /// `Request` was explicitly cancelled.
  157. case explicitlyCancelled
  158. /// `URLConvertible` type failed to create a valid `URL`.
  159. case invalidURL(url: URLConvertible)
  160. /// Multipart form encoding failed.
  161. case multipartEncodingFailed(reason: MultipartEncodingFailureReason)
  162. /// `ParameterEncoding` threw an error during the encoding process.
  163. case parameterEncodingFailed(reason: ParameterEncodingFailureReason)
  164. /// `ParameterEncoder` threw an error while running the encoder.
  165. case parameterEncoderFailed(reason: ParameterEncoderFailureReason)
  166. /// `RequestAdapter` failed threw an error during adaptation.
  167. case requestAdaptationFailed(error: Error)
  168. /// `RequestRetrier` threw an error during the request retry process.
  169. case requestRetryFailed(retryError: Error, originalError: Error)
  170. /// Response validation failed.
  171. case responseValidationFailed(reason: ResponseValidationFailureReason)
  172. /// Response serialization failed.
  173. case responseSerializationFailed(reason: ResponseSerializationFailureReason)
  174. /// `ServerTrustEvaluating` instance threw an error during trust evaluation.
  175. case serverTrustEvaluationFailed(reason: ServerTrustFailureReason)
  176. /// `Session` which issued the `Request` was deinitialized, most likely because its reference went out of scope.
  177. case sessionDeinitialized
  178. /// `Session` was explicitly invalidated, possibly with the `Error` produced by the underlying `URLSession`.
  179. case sessionInvalidated(error: Error?)
  180. /// `URLRequest` failed validation.
  181. case urlRequestValidationFailed(reason: URLRequestValidationFailureReason)
  182. }
  183. extension Error {
  184. /// Returns the instance cast as an `AFError`.
  185. public var asAFError: AFError? {
  186. return self as? AFError
  187. }
  188. }
  189. // MARK: - Error Booleans
  190. extension AFError {
  191. /// Returns whether the instance is `.sessionDeinitialized`.
  192. public var isSessionDeinitializedError: Bool {
  193. if case .sessionDeinitialized = self { return true }
  194. return false
  195. }
  196. /// Returns whether the instance is `.sessionInvalidated`.
  197. public var isSessionInvalidatedError: Bool {
  198. if case .sessionInvalidated = self { return true }
  199. return false
  200. }
  201. /// Returns whether the instance is `.explicitlyCancelled`.
  202. public var isExplicitlyCancelledError: Bool {
  203. if case .explicitlyCancelled = self { return true }
  204. return false
  205. }
  206. /// Returns whether the instance is `.invalidURL`.
  207. public var isInvalidURLError: Bool {
  208. if case .invalidURL = self { return true }
  209. return false
  210. }
  211. /// Returns whether the instance is `.parameterEncodingFailed`. When `true`, the `underlyingError` property will
  212. /// contain the associated value.
  213. public var isParameterEncodingError: Bool {
  214. if case .parameterEncodingFailed = self { return true }
  215. return false
  216. }
  217. /// Returns whether the instance is `.parameterEncoderFailed`. When `true`, the `underlyingError` property will
  218. /// contain the associated value.
  219. public var isParameterEncoderError: Bool {
  220. if case .parameterEncoderFailed = self { return true }
  221. return false
  222. }
  223. /// Returns whether the instance is `.multipartEncodingFailed`. When `true`, the `url` and `underlyingError`
  224. /// properties will contain the associated values.
  225. public var isMultipartEncodingError: Bool {
  226. if case .multipartEncodingFailed = self { return true }
  227. return false
  228. }
  229. /// Returns whether the instance is `.requestAdaptationFailed`. When `true`, the `underlyingError` property will
  230. /// contain the associated value.
  231. public var isRequestAdaptationError: Bool {
  232. if case .requestAdaptationFailed = self { return true }
  233. return false
  234. }
  235. /// Returns whether the instance is `.responseValidationFailed`. When `true`, the `acceptableContentTypes`,
  236. /// `responseContentType`, and `responseCode` properties will contain the associated values.
  237. public var isResponseValidationError: Bool {
  238. if case .responseValidationFailed = self { return true }
  239. return false
  240. }
  241. /// Returns whether the instance is `.responseSerializationFailed`. When `true`, the `failedStringEncoding` and
  242. /// `underlyingError` properties will contain the associated values.
  243. public var isResponseSerializationError: Bool {
  244. if case .responseSerializationFailed = self { return true }
  245. return false
  246. }
  247. /// Returns whether the instance is `.serverTrustEvaluationFailed`.
  248. public var isServerTrustEvaluationError: Bool {
  249. if case .serverTrustEvaluationFailed = self { return true }
  250. return false
  251. }
  252. /// Returns whether the instance is `requestRetryFailed`. When `true`, the `underlyingError` property will
  253. /// contain the associated value.
  254. public var isRequestRetryError: Bool {
  255. if case .requestRetryFailed = self { return true }
  256. return false
  257. }
  258. }
  259. // MARK: - Convenience Properties
  260. extension AFError {
  261. /// The `URLConvertible` associated with the error.
  262. public var urlConvertible: URLConvertible? {
  263. guard case .invalidURL(let url) = self else { return nil }
  264. return url
  265. }
  266. /// The `URL` associated with the error.
  267. public var url: URL? {
  268. guard case .multipartEncodingFailed(let reason) = self else { return nil }
  269. return reason.url
  270. }
  271. /// The underlying `Error` responsible for generating the failure associated with `.sessionInvalidated`,
  272. /// `.parameterEncodingFailed`, `.parameterEncoderFailed`, `.multipartEncodingFailed`, `.requestAdaptationFailed`,
  273. /// `.responseSerializationFailed`, `.requestRetryFailed` errors.
  274. public var underlyingError: Error? {
  275. switch self {
  276. case .sessionInvalidated(let error):
  277. return error
  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. guard case .responseValidationFailed(let reason) = self else { return nil }
  297. return reason.acceptableContentTypes
  298. }
  299. /// The response `Content-Type` of a `.responseValidationFailed` error.
  300. public var responseContentType: String? {
  301. guard case .responseValidationFailed(let reason) = self else { return nil }
  302. return reason.responseContentType
  303. }
  304. /// The response code of a `.responseValidationFailed` error.
  305. public var responseCode: Int? {
  306. guard case .responseValidationFailed(let reason) = self else { return nil }
  307. return reason.responseCode
  308. }
  309. /// The `String.Encoding` associated with a failed `.stringResponse()` call.
  310. public var failedStringEncoding: String.Encoding? {
  311. guard case .responseSerializationFailed(let reason) = self else { return nil }
  312. return reason.failedStringEncoding
  313. }
  314. }
  315. extension AFError.ParameterEncodingFailureReason {
  316. var underlyingError: Error? {
  317. guard case .jsonEncodingFailed(let error) = self else { return nil }
  318. return error
  319. }
  320. }
  321. extension AFError.ParameterEncoderFailureReason {
  322. var underlyingError: Error? {
  323. guard case .encoderFailed(let error) = self else { return nil }
  324. return error
  325. }
  326. }
  327. extension AFError.MultipartEncodingFailureReason {
  328. var url: URL? {
  329. switch self {
  330. case .bodyPartURLInvalid(let url), .bodyPartFilenameInvalid(let url), .bodyPartFileNotReachable(let url),
  331. .bodyPartFileIsDirectory(let url), .bodyPartFileSizeNotAvailable(let url),
  332. .bodyPartInputStreamCreationFailed(let url), .outputStreamCreationFailed(let url),
  333. .outputStreamFileAlreadyExists(let url), .outputStreamURLInvalid(let url),
  334. .bodyPartFileNotReachableWithError(let url, _), .bodyPartFileSizeQueryFailedWithError(let url, _):
  335. return url
  336. default:
  337. return nil
  338. }
  339. }
  340. var underlyingError: Error? {
  341. switch self {
  342. case .bodyPartFileNotReachableWithError(_, let error), .bodyPartFileSizeQueryFailedWithError(_, let error),
  343. .outputStreamWriteFailed(let error), .inputStreamReadFailed(let error):
  344. return error
  345. default:
  346. return nil
  347. }
  348. }
  349. }
  350. extension AFError.ResponseValidationFailureReason {
  351. var acceptableContentTypes: [String]? {
  352. switch self {
  353. case .missingContentType(let types), .unacceptableContentType(let types, _):
  354. return types
  355. default:
  356. return nil
  357. }
  358. }
  359. var responseContentType: String? {
  360. guard case .unacceptableContentType(_, let responseType) = self else { return nil }
  361. return responseType
  362. }
  363. var responseCode: Int? {
  364. guard case .unacceptableStatusCode(let code) = self else { return nil }
  365. return code
  366. }
  367. }
  368. extension AFError.ResponseSerializationFailureReason {
  369. var failedStringEncoding: String.Encoding? {
  370. guard case .stringSerializationFailed(let encoding) = self else { return nil }
  371. return encoding
  372. }
  373. var underlyingError: Error? {
  374. guard case .jsonSerializationFailed(let error) = self else { return nil }
  375. return error
  376. }
  377. }
  378. extension AFError.ServerTrustFailureReason {
  379. var output: AFError.ServerTrustFailureReason.Output? {
  380. switch self {
  381. case let .defaultEvaluationFailed(output),
  382. let .hostValidationFailed(output),
  383. let .revocationCheckFailed(output, _):
  384. return output
  385. default: return nil
  386. }
  387. }
  388. }
  389. // MARK: - Error Descriptions
  390. extension AFError: LocalizedError {
  391. public var errorDescription: String? {
  392. switch self {
  393. case .explicitlyCancelled:
  394. return "Request explicitly cancelled."
  395. case .invalidURL(let url):
  396. return "URL is not valid: \(url)"
  397. case .parameterEncodingFailed(let reason):
  398. return reason.localizedDescription
  399. case .parameterEncoderFailed(let reason):
  400. return reason.localizedDescription
  401. case .multipartEncodingFailed(let reason):
  402. return reason.localizedDescription
  403. case .requestAdaptationFailed(let error):
  404. return "Request adaption failed with error: \(error.localizedDescription)"
  405. case .responseValidationFailed(let reason):
  406. return reason.localizedDescription
  407. case .responseSerializationFailed(let reason):
  408. return reason.localizedDescription
  409. case .requestRetryFailed(let retryError, let originalError):
  410. return """
  411. Request retry failed with retry error: \(retryError.localizedDescription), \
  412. original error: \(originalError.localizedDescription)
  413. """
  414. case .sessionDeinitialized:
  415. return """
  416. Session was invalidated without error, so it was likely deinitialized unexpectedly. \
  417. Be sure to retain a reference to your Session for the duration of your requests.
  418. """
  419. case .sessionInvalidated(let error):
  420. return "Session was invalidated with error: \(error?.localizedDescription ?? "No description.")"
  421. case .serverTrustEvaluationFailed:
  422. return "Server trust evaluation failed."
  423. case let .urlRequestValidationFailed(reason):
  424. return "URLRequest validation failed due to reason: \(reason.localizedDescription)"
  425. }
  426. }
  427. }
  428. extension AFError.ParameterEncodingFailureReason {
  429. var localizedDescription: String {
  430. switch self {
  431. case .missingURL:
  432. return "URL request to encode was missing a URL"
  433. case .jsonEncodingFailed(let error):
  434. return "JSON could not be encoded because of error:\n\(error.localizedDescription)"
  435. }
  436. }
  437. }
  438. extension AFError.ParameterEncoderFailureReason {
  439. var localizedDescription: String {
  440. switch self {
  441. case .missingRequiredComponent(let component):
  442. return "Encoding failed due to a missing request component: \(component)"
  443. case .encoderFailed(let error):
  444. return "The underlying encoder failed with the error: \(error)"
  445. }
  446. }
  447. }
  448. extension AFError.MultipartEncodingFailureReason {
  449. var localizedDescription: String {
  450. switch self {
  451. case .bodyPartURLInvalid(let url):
  452. return "The URL provided is not a file URL: \(url)"
  453. case .bodyPartFilenameInvalid(let url):
  454. return "The URL provided does not have a valid filename: \(url)"
  455. case .bodyPartFileNotReachable(let url):
  456. return "The URL provided is not reachable: \(url)"
  457. case .bodyPartFileNotReachableWithError(let url, let error):
  458. return (
  459. "The system returned an error while checking the provided URL for " +
  460. "reachability.\nURL: \(url)\nError: \(error)"
  461. )
  462. case .bodyPartFileIsDirectory(let url):
  463. return "The URL provided is a directory: \(url)"
  464. case .bodyPartFileSizeNotAvailable(let url):
  465. return "Could not fetch the file size from the provided URL: \(url)"
  466. case .bodyPartFileSizeQueryFailedWithError(let url, let error):
  467. return (
  468. "The system returned an error while attempting to fetch the file size from the " +
  469. "provided URL.\nURL: \(url)\nError: \(error)"
  470. )
  471. case .bodyPartInputStreamCreationFailed(let url):
  472. return "Failed to create an InputStream for the provided URL: \(url)"
  473. case .outputStreamCreationFailed(let url):
  474. return "Failed to create an OutputStream for URL: \(url)"
  475. case .outputStreamFileAlreadyExists(let url):
  476. return "A file already exists at the provided URL: \(url)"
  477. case .outputStreamURLInvalid(let url):
  478. return "The provided OutputStream URL is invalid: \(url)"
  479. case .outputStreamWriteFailed(let error):
  480. return "OutputStream write failed with error: \(error)"
  481. case .inputStreamReadFailed(let error):
  482. return "InputStream read failed with error: \(error)"
  483. }
  484. }
  485. }
  486. extension AFError.ResponseSerializationFailureReason {
  487. var localizedDescription: String {
  488. switch self {
  489. case .inputDataNilOrZeroLength:
  490. return "Response could not be serialized, input data was nil or zero length."
  491. case .inputFileNil:
  492. return "Response could not be serialized, input file was nil."
  493. case .inputFileReadFailed(let url):
  494. return "Response could not be serialized, input file could not be read: \(url)."
  495. case .stringSerializationFailed(let encoding):
  496. return "String could not be serialized with encoding: \(encoding)."
  497. case .jsonSerializationFailed(let error):
  498. return "JSON could not be serialized because of error:\n\(error.localizedDescription)"
  499. case .invalidEmptyResponse(let type):
  500. return "Empty response could not be serialized to type: \(type). Use Empty as the expected type for such responses."
  501. case .decodingFailed(let error):
  502. return "Response could not be decoded because of error:\n\(error.localizedDescription)"
  503. }
  504. }
  505. }
  506. extension AFError.ResponseValidationFailureReason {
  507. var localizedDescription: String {
  508. switch self {
  509. case .dataFileNil:
  510. return "Response could not be validated, data file was nil."
  511. case .dataFileReadFailed(let url):
  512. return "Response could not be validated, data file could not be read: \(url)."
  513. case .missingContentType(let types):
  514. return (
  515. "Response Content-Type was missing and acceptable content types " +
  516. "(\(types.joined(separator: ","))) do not match \"*/*\"."
  517. )
  518. case .unacceptableContentType(let acceptableTypes, let responseType):
  519. return (
  520. "Response Content-Type \"\(responseType)\" does not match any acceptable types: " +
  521. "\(acceptableTypes.joined(separator: ","))."
  522. )
  523. case .unacceptableStatusCode(let code):
  524. return "Response status code was unacceptable: \(code)."
  525. }
  526. }
  527. }
  528. extension AFError.ServerTrustFailureReason {
  529. var localizedDescription: String {
  530. switch self {
  531. case let .noRequiredEvaluator(host):
  532. return "A ServerTrustEvaluating value is required for host \(host) but none was found."
  533. case .noCertificatesFound:
  534. return "No certificates were found or provided for evaluation."
  535. case .noPublicKeysFound:
  536. return "No public keys were found or provided for evaluation."
  537. case .policyApplicationFailed:
  538. return "Attempting to set a SecPolicy failed."
  539. case .settingAnchorCertificatesFailed:
  540. return "Attempting to set the provided certificates as anchor certificates failed."
  541. case .revocationPolicyCreationFailed:
  542. return "Attempting to create a revocation policy failed."
  543. case let .defaultEvaluationFailed(output):
  544. return "Default evaluation failed for host \(output.host)."
  545. case let .hostValidationFailed(output):
  546. return "Host validation failed for host \(output.host)."
  547. case let .revocationCheckFailed(output, _):
  548. return "Revocation check failed for host \(output.host)."
  549. case let .certificatePinningFailed(host, _, _, _):
  550. return "Certificate pinning failed for host \(host)."
  551. case let .publicKeyPinningFailed(host, _, _, _):
  552. return "Public key pinning failed for host \(host)."
  553. }
  554. }
  555. }
  556. extension AFError.URLRequestValidationFailureReason {
  557. var localizedDescription: String {
  558. switch self {
  559. case let .bodyDataInGETRequest(data):
  560. return """
  561. Invalid URLRequest with a GET method that had body data:
  562. \(String(decoding: data, as: UTF8.self))
  563. """
  564. }
  565. }
  566. }