KingfisherError.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. //
  2. // KingfisherError.swift
  3. // Kingfisher
  4. //
  5. // Created by onevcat on 2018/09/26.
  6. //
  7. // Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import Foundation
  27. #if os(macOS)
  28. import AppKit
  29. #else
  30. import UIKit
  31. #endif
  32. extension Never {}
  33. /// Represents all the errors that can occur in the Kingfisher framework.
  34. ///
  35. /// Kingfisher-related methods always throw a ``KingfisherError`` or invoke the callback with ``KingfisherError``
  36. /// as its error type. To handle errors from Kingfisher, you switch over the error to get a reason catalog,
  37. /// then switch over the reason to understand the error details.
  38. ///
  39. public enum KingfisherError: Error {
  40. // MARK: Error Reason Types
  41. /// Represents the error reasons during the networking request phase.
  42. public enum RequestErrorReason: Sendable {
  43. /// The request is empty.
  44. ///
  45. /// Error Code: 1001
  46. case emptyRequest
  47. /// The URL of the request is invalid.
  48. ///
  49. /// - Parameter request: The request is intended to be sent, but its URL is invalid.
  50. ///
  51. /// Error Code: 1002
  52. case invalidURL(request: URLRequest)
  53. /// The downloading task is canceled by the user.
  54. ///
  55. /// - Parameters:
  56. /// - task: The session data task which is canceled.
  57. /// - token: The cancel token which is used for canceling the task.
  58. ///
  59. /// Error Code: 1003
  60. case taskCancelled(task: SessionDataTask, token: SessionDataTask.CancelToken)
  61. /// The live photo downloading task is canceled by the user.
  62. ///
  63. /// - Parameters:
  64. /// - source: The live phot source.
  65. ///
  66. /// Error Code: 1004
  67. case livePhotoTaskCancelled(source: LivePhotoSource)
  68. case asyncTaskContextCancelled
  69. }
  70. /// Represents the error reason during networking response phase.
  71. public enum ResponseErrorReason: Sendable {
  72. /// The response is not a valid URL response.
  73. ///
  74. /// - Parameters:
  75. /// - response: The received invalid URL response.
  76. /// The response is expected to be an HTTP response, but it is not.
  77. ///
  78. /// Error Code: 2001
  79. case invalidURLResponse(response: URLResponse)
  80. /// The response contains an invalid HTTP status code.
  81. ///
  82. /// - Parameters:
  83. /// - response: The received response.
  84. ///
  85. /// Error Code: 2002
  86. ///
  87. /// - Note: By default, status code 200..<400 is recognized as valid. You can override
  88. /// this behavior by conforming to the `ImageDownloaderDelegate`.
  89. case invalidHTTPStatusCode(response: HTTPURLResponse)
  90. /// An error happens in the system URL session.
  91. ///
  92. /// - Parameters:
  93. /// - error: The underlying URLSession error object.
  94. ///
  95. /// Error Code: 2003
  96. case URLSessionError(error: any Error)
  97. /// Data modifying fails on returning a valid data.
  98. ///
  99. /// - Parameters:
  100. /// - task: The failed task.
  101. ///
  102. /// Error Code: 2004
  103. case dataModifyingFailed(task: SessionDataTask)
  104. /// The task is done but no URL response found.
  105. ///
  106. /// - Parameters:
  107. /// - task: The failed task.
  108. ///
  109. /// Error Code: 2005
  110. case noURLResponse(task: SessionDataTask)
  111. /// The task is cancelled by ``ImageDownloaderDelegate`` due to the `.cancel` response disposition is
  112. /// specified by the delegate method.
  113. ///
  114. /// - Parameters:
  115. /// - task: The cancelled task.
  116. ///
  117. /// Error Code: 2006
  118. case cancelledByDelegate(response: URLResponse)
  119. }
  120. /// Represents the error reason during Kingfisher caching.
  121. public enum CacheErrorReason: @unchecked Sendable {
  122. /// Cannot create a file enumerator for a certain disk URL.
  123. ///
  124. /// - Parameters:
  125. /// - url: The target disk URL from which the file enumerator should be created.
  126. ///
  127. /// Error Code: 3001
  128. case fileEnumeratorCreationFailed(url: URL)
  129. /// Cannot get correct file contents from a file enumerator.
  130. ///
  131. /// - Parameters:
  132. /// - url: The target disk URL from which the content of a file enumerator should be obtained.
  133. ///
  134. /// Error Code: 3002
  135. case invalidFileEnumeratorContent(url: URL)
  136. /// The file at the target URL exists, but its URL resource is unavailable.
  137. ///
  138. /// - Parameters:
  139. /// - error: The underlying error thrown by the file manager.
  140. /// - key: The key used to retrieve the resource from cache.
  141. /// - url: The disk URL where the target cached file exists.
  142. ///
  143. /// Error Code: 3003
  144. case invalidURLResource(error: any Error, key: String, url: URL)
  145. /// The file at the target URL exists, but the data cannot be loaded from it.
  146. ///
  147. /// - Parameters:
  148. /// - url: The disk URL where the target cached file exists.
  149. /// - error: The underlying error that describes why this error occurs.
  150. ///
  151. /// Error Code: 3004
  152. case cannotLoadDataFromDisk(url: URL, error: any Error)
  153. /// Cannot create a folder at a given path.
  154. ///
  155. /// - Parameters:
  156. /// - path: The disk path where the directory creation operation fails.
  157. /// - error: The underlying error that describes why this error occurs.
  158. ///
  159. /// Error Code: 3005
  160. case cannotCreateDirectory(path: String, error: any Error)
  161. /// The requested image does not exist in the cache.
  162. ///
  163. /// - Parameters:
  164. /// - key: The key of the requested image in the cache.
  165. ///
  166. /// Error Code: 3006
  167. case imageNotExisting(key: String)
  168. /// Unable to convert an object to data for storage.
  169. ///
  170. /// - Parameters:
  171. /// - object: The object that needs to be converted to data.
  172. ///
  173. /// Error Code: 3007
  174. case cannotConvertToData(object: Any, error: any Error)
  175. /// Unable to serialize an image to data for storage.
  176. ///
  177. /// - Parameters:
  178. /// - image: The input image that needs to be serialized to cache.
  179. /// - original: The original image data, if it exists.
  180. /// - serializer: The ``CacheSerializer`` used for the image serialization.
  181. ///
  182. /// Error Code: 3008
  183. case cannotSerializeImage(image: KFCrossPlatformImage?, original: Data?, serializer: any CacheSerializer)
  184. /// Unable to create the cache file at a specified `fileURL` under a given `key`.
  185. ///
  186. /// - Parameters:
  187. /// - fileURL: The URL where the cache file should be created.
  188. /// - key: The cache key used for the cache. When caching a file through ``KingfisherManager`` and Kingfisher's
  189. /// extension method, it is the resolved cache key based on your input ``Source`` and the image
  190. /// processors.
  191. /// - data: The data to be cached.
  192. /// - error: The underlying error originally thrown by Foundation when attempting to write the `data` to the disk file at
  193. /// `fileURL`.
  194. ///
  195. /// Error Code: 3009
  196. case cannotCreateCacheFile(fileURL: URL, key: String, data: Data, error: any Error)
  197. /// Unable to set file attributes for a cached file.
  198. ///
  199. /// - Parameters:
  200. /// - filePath: The path of the target cache file.
  201. /// - attributes: The file attributes to be set for the target file.
  202. /// - error: The underlying error originally thrown by the Foundation framework when attempting to set the specified
  203. /// `attributes` for the disk file at `filePath`.
  204. ///
  205. /// Error Code: 3010
  206. case cannotSetCacheFileAttribute(filePath: String, attributes: [FileAttributeKey : Any], error: any Error)
  207. /// The disk storage for caching is not ready.
  208. ///
  209. /// - Parameters:
  210. /// - cacheURL: The intended URL that should be the storage folder.
  211. ///
  212. /// This issue typically arises due to an extreme lack of space on the disk storage. Kingfisher fails to create
  213. /// the cache folder under these circumstances, rendering the disk storage unusable. In such cases, it is
  214. /// recommended to prompt the user to free up storage space and restart the app to restore functionality.
  215. ///
  216. /// Error Code: 3011
  217. case diskStorageIsNotReady(cacheURL: URL)
  218. /// The resource is expected on the disk, but now missing for some reason.
  219. ///
  220. /// This happens when the expected resource is not on the disk for some reason during loading a live photo.
  221. ///
  222. /// Error Code: 3012
  223. case missingLivePhotoResourceOnDisk(_ resource: LivePhotoResource)
  224. }
  225. /// Represents the error reason during image processing phase.
  226. public enum ProcessorErrorReason: Sendable {
  227. /// Image processing has failed, and there is no valid output image generated by the processor.
  228. ///
  229. /// - Parameters:
  230. /// - processor: The `ImageProcessor` responsible for processing the image or its data in `item`.
  231. /// - item: The image or its data content.
  232. ///
  233. /// Error Code: 4001
  234. case processingFailed(processor: any ImageProcessor, item: ImageProcessItem)
  235. }
  236. /// Represents the error reason during image setting in a view related class.
  237. public enum ImageSettingErrorReason: Sendable {
  238. /// The input resource is empty or `nil`.
  239. ///
  240. /// Error Code: 5001
  241. case emptySource
  242. /// The resource task is completed, but it is not the one that was expected. This typically occurs when you set
  243. /// another resource on the view without canceling the current ongoing task. The previous task will fail with the
  244. /// `.notCurrentSourceTask` error when a result is obtained, regardless of whether it was successful or not for
  245. /// that task.
  246. ///
  247. /// - Parameters:
  248. /// - result: The `RetrieveImageResult` if the source task is completed without any issues. `nil` if an error occurred.
  249. /// - error: The `Error` if there was a problem during the image setting task. `nil` if the task completed successfully.
  250. /// - source: The original source value of the task.
  251. ///
  252. /// Error Code: 5002
  253. case notCurrentSourceTask(result: RetrieveImageResult?, error: (any Error)?, source: Source)
  254. /// An error occurs while retrieving data from an `ImageDataProvider`.
  255. ///
  256. /// - Parameters:
  257. /// - provider: The ``ImageDataProvider`` that encountered the error.
  258. /// - error: The underlying error that describes why this error occurred.
  259. ///
  260. /// Error Code: 5003
  261. case dataProviderError(provider: any ImageDataProvider, error: any Error)
  262. /// No more alternative ``Source`` can be used in current loading process. It means that the
  263. /// ``KingfisherOptionsInfoItem/alternativeSources(_:)`` are set and Kingfisher tried to recovery from the original error, but still
  264. /// fails for all the given alternative sources. The associated value holds all the errors encountered during
  265. /// the load process, including the original source loading error and all the alternative sources errors.
  266. /// Code 5004.
  267. /// No more alternative `Source` can be used in the current loading process.
  268. ///
  269. /// - Parameters:
  270. /// - error : A ``PropagationError`` contains more information about the source and error.
  271. ///
  272. /// This means that the ``KingfisherOptionsInfoItem/alternativeSources(_:)`` option is set, and Kingfisher attempted to recover from the original error,
  273. /// but still failed for all the provided alternative sources. The associated value holds all the errors encountered during
  274. /// the loading process, including the original source loading error and all the alternative sources errors.
  275. ///
  276. /// Error Code: 5004
  277. case alternativeSourcesExhausted([PropagationError])
  278. /// The resource task is completed, but it is not the one that was expected. This typically occurs when you set
  279. /// another resource on the view without canceling the current ongoing task. The previous task will fail with the
  280. /// `.notCurrentLivePhotoSourceTask` error when a result is obtained, regardless of whether it was successful or
  281. /// not for that task.
  282. ///
  283. /// This error is the live photo version of the `.notCurrentSourceTask` error (error 5002).
  284. ///
  285. /// - Parameters:
  286. /// - result: The `RetrieveImageResult` if the source task is completed without any issues. `nil` if an error occurred.
  287. /// - error: The `Error` if there was a problem during the image setting task. `nil` if the task completed successfully.
  288. /// - source: The original source value of the task.
  289. ///
  290. /// Error Code: 5005
  291. case notCurrentLivePhotoSourceTask(
  292. result: RetrieveLivePhotoResult?, error: (any Error)?, source: LivePhotoSource
  293. )
  294. /// The error happens during processing the live photo.
  295. ///
  296. /// When creating the final `PHLivePhoto` object from the downloaded image files, the internal Photos framework
  297. /// method `PHLivePhoto.request(withResourceFileURLs:placeholderImage:targetSize:contentMode:resultHandler:)`
  298. /// invokes its `resultHandler`. If the `info` dictionary in `resultHandler` contains `PHLivePhotoInfoErrorKey`,
  299. /// Kingfisher raises this error reason to pass the information to outside.
  300. ///
  301. /// If the processing fails due to any error that is not a `KingfisherError` case, Kingfisher also reports it
  302. /// with this reason.
  303. ///
  304. /// - Parameters:
  305. /// - result: The `RetrieveLivePhotoResult` if the source task is completed and a result is already existing.
  306. /// - error: The `NSError` if `PHLivePhotoInfoErrorKey` is contained in the `resultHandler` info dictionary.
  307. /// - source: The original source value of the task.
  308. ///
  309. /// - Note: It is possible that both `result` and `error` are non-nil value. Check the
  310. /// ``RetrieveLivePhotoResult/info`` property for the raw values that are from the Photos framework.
  311. ///
  312. /// Error Code: 5006
  313. case livePhotoResultError(result: RetrieveLivePhotoResult?, error: (any Error)?, source: LivePhotoSource)
  314. }
  315. // MARK: Member Cases
  316. /// Represents the error reasons that can occur during the networking request phase.
  317. case requestError(reason: RequestErrorReason)
  318. /// Represents the error reason that can occur during networking response phase.
  319. case responseError(reason: ResponseErrorReason)
  320. /// Represents the error reason that can occur during Kingfisher caching phase.
  321. case cacheError(reason: CacheErrorReason)
  322. /// Represents the error reason that can occur during image processing phase.
  323. case processorError(reason: ProcessorErrorReason)
  324. /// Represents the error reason that can occur during image setting in a view related class.
  325. case imageSettingError(reason: ImageSettingErrorReason)
  326. // MARK: Helper Properties & Methods
  327. /// A helper property to determine if this error is of type `RequestErrorReason.taskCancelled`.
  328. public var isTaskCancelled: Bool {
  329. if case .requestError(reason: .taskCancelled) = self {
  330. return true
  331. }
  332. return false
  333. }
  334. /// Helper method to check whether this error is a ``ResponseErrorReason/invalidHTTPStatusCode(response:)``
  335. /// and the associated value is a given status code.
  336. ///
  337. /// - Parameter code: The given status code.
  338. /// - Returns: If `self` is a `ResponseErrorReason.invalidHTTPStatusCode` error
  339. /// and its status code equals to `code`, `true` is returned. Otherwise, `false`.
  340. ///
  341. /// A helper method for checking HTTP status code.
  342. ///
  343. /// Use this helper method to determine whether this error corresponds to a
  344. /// ``ResponseErrorReason/invalidHTTPStatusCode(response:)`` with a specific status code.
  345. ///
  346. /// - Parameter code: The desired HTTP status code for comparison.
  347. /// - Returns: `true` if the error is of type ``ResponseErrorReason/invalidHTTPStatusCode(response:)`` and its
  348. /// status code matches the provided `code`, otherwise `false`.
  349. public func isInvalidResponseStatusCode(_ code: Int) -> Bool {
  350. if case .responseError(reason: .invalidHTTPStatusCode(let response)) = self {
  351. return response.statusCode == code
  352. }
  353. return false
  354. }
  355. /// A helper method for checking the error is type of ``ResponseErrorReason/invalidHTTPStatusCode(response:)``.
  356. public var isInvalidResponseStatusCode: Bool {
  357. if case .responseError(reason: .invalidHTTPStatusCode) = self {
  358. return true
  359. }
  360. return false
  361. }
  362. /// A helper property that indicates whether this error is of type
  363. /// ``ImageSettingErrorReason/notCurrentSourceTask(result:error:source:)`` or not.
  364. ///
  365. /// This property is used to check if a new image setting task starts while the old one is still running.
  366. /// In such a scenario, the identifier of the new task will overwrite the identifier of the old task.
  367. ///
  368. /// When the old task finishes, a ``ImageSettingErrorReason/notCurrentSourceTask(result:error:source:)`` error will
  369. /// be raised to notify you that the setting process has completed with a certain result, but the image view or
  370. /// button has not been updated.
  371. ///
  372. /// - Returns: `true` if the error is of type ``ImageSettingErrorReason/notCurrentSourceTask(result:error:source:)``,
  373. /// `false` otherwise.
  374. public var isNotCurrentTask: Bool {
  375. if case .imageSettingError(reason: .notCurrentSourceTask(_, _, _)) = self {
  376. return true
  377. }
  378. return false
  379. }
  380. var isLowDataModeConstrained: Bool {
  381. if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *),
  382. case .responseError(reason: .URLSessionError(let sessionError)) = self,
  383. let urlError = sessionError as? URLError,
  384. urlError.networkUnavailableReason == .constrained
  385. {
  386. return true
  387. }
  388. return false
  389. }
  390. }
  391. // MARK: - LocalizedError Conforming
  392. extension KingfisherError: LocalizedError {
  393. /// Provides a localized message describing the error that occurred.
  394. ///
  395. /// Use this property to obtain a human-readable description of the error for display to the user.
  396. public var errorDescription: String? {
  397. switch self {
  398. case .requestError(let reason): return reason.errorDescription
  399. case .responseError(let reason): return reason.errorDescription
  400. case .cacheError(let reason): return reason.errorDescription
  401. case .processorError(let reason): return reason.errorDescription
  402. case .imageSettingError(let reason): return reason.errorDescription
  403. }
  404. }
  405. }
  406. // MARK: - CustomNSError Conforming
  407. extension KingfisherError: CustomNSError {
  408. /// The error domain for ``KingfisherError``. All errors generated by Kingfisher are categorized under this domain.
  409. ///
  410. /// When handling errors from the Kingfisher library, you can use this domain to identify and distinguish them
  411. /// from other types of errors in your application.
  412. ///
  413. /// - Note: The error domain is a string identifier associated with each error.
  414. public static let domain = "com.onevcat.Kingfisher.Error"
  415. /// Represents the error code within the specified error domain.
  416. ///
  417. /// Use this property to retrieve the specific error code associated with a ``KingfisherError``. The error code
  418. /// provides additional context and information about the error, allowing you to handle and respond to different
  419. /// error scenarios.
  420. ///
  421. /// - Note: Error codes are numerical values associated with each error within a domain. Check the error code in the
  422. /// API reference of each error reason for the detail.
  423. ///
  424. /// - Returns: The error code as an integer.
  425. public var errorCode: Int {
  426. switch self {
  427. case .requestError(let reason): return reason.errorCode
  428. case .responseError(let reason): return reason.errorCode
  429. case .cacheError(let reason): return reason.errorCode
  430. case .processorError(let reason): return reason.errorCode
  431. case .imageSettingError(let reason): return reason.errorCode
  432. }
  433. }
  434. }
  435. extension KingfisherError.RequestErrorReason {
  436. var errorDescription: String? {
  437. switch self {
  438. case .emptyRequest:
  439. return "The request is empty or `nil`."
  440. case .invalidURL(let request):
  441. return "The request contains an invalid or empty URL. Request: \(request)."
  442. case .taskCancelled(let task, let token):
  443. return "The session task was cancelled. Task: \(task), cancel token: \(token)."
  444. case .livePhotoTaskCancelled(let source):
  445. return "The live photo download task was cancelled. Source: \(source)"
  446. case .asyncTaskContextCancelled:
  447. return "The async task context was cancelled. This usually happens when the task is cancelled before it starts."
  448. }
  449. }
  450. var errorCode: Int {
  451. switch self {
  452. case .emptyRequest: return 1001
  453. case .invalidURL: return 1002
  454. case .taskCancelled: return 1003
  455. case .livePhotoTaskCancelled: return 1004
  456. case .asyncTaskContextCancelled: return 1005
  457. }
  458. }
  459. }
  460. extension KingfisherError.ResponseErrorReason {
  461. var errorDescription: String? {
  462. switch self {
  463. case .invalidURLResponse(let response):
  464. return "The URL response is invalid: \(response)"
  465. case .invalidHTTPStatusCode(let response):
  466. return "The HTTP status code in response is invalid. Code: \(response.statusCode), response: \(response)."
  467. case .URLSessionError(let error):
  468. return "A URL session error happened. The underlying error: \(error)"
  469. case .dataModifyingFailed(let task):
  470. return "The data modifying delegate returned `nil` for the downloaded data. Task: \(task)."
  471. case .noURLResponse(let task):
  472. return "No URL response received. Task: \(task)."
  473. case .cancelledByDelegate(let response):
  474. return "The downloading task is cancelled by the downloader delegate. Response: \(response)."
  475. }
  476. }
  477. var errorCode: Int {
  478. switch self {
  479. case .invalidURLResponse: return 2001
  480. case .invalidHTTPStatusCode: return 2002
  481. case .URLSessionError: return 2003
  482. case .dataModifyingFailed: return 2004
  483. case .noURLResponse: return 2005
  484. case .cancelledByDelegate: return 2006
  485. }
  486. }
  487. }
  488. extension KingfisherError.CacheErrorReason {
  489. var errorDescription: String? {
  490. switch self {
  491. case .fileEnumeratorCreationFailed(let url):
  492. return "Cannot create file enumerator for URL: \(url)."
  493. case .invalidFileEnumeratorContent(let url):
  494. return "Cannot get contents from the file enumerator at URL: \(url)."
  495. case .invalidURLResource(let error, let key, let url):
  496. return "Cannot get URL resource values or data for the given URL: \(url). " +
  497. "Cache key: \(key). Underlying error: \(error)"
  498. case .cannotLoadDataFromDisk(let url, let error):
  499. return "Cannot load data from disk at URL: \(url). Underlying error: \(error)"
  500. case .cannotCreateDirectory(let path, let error):
  501. return "Cannot create directory at given path: Path: \(path). Underlying error: \(error)"
  502. case .imageNotExisting(let key):
  503. return "The image is not in cache, but you requires it should only be " +
  504. "from cache by enabling the `.onlyFromCache` option. Key: \(key)."
  505. case .cannotConvertToData(let object, let error):
  506. return "Cannot convert the input object to a `Data` object when storing it to disk cache. " +
  507. "Object: \(object). Underlying error: \(error)"
  508. case .cannotSerializeImage(let image, let originalData, let serializer):
  509. return "Cannot serialize an image due to the cache serializer returning `nil`. " +
  510. "Image: \(String(describing:image)), original data: \(String(describing: originalData)), " +
  511. "serializer: \(serializer)."
  512. case .cannotCreateCacheFile(let fileURL, let key, let data, let error):
  513. return "Cannot create cache file at url: \(fileURL), key: \(key), data length: \(data.count). " +
  514. "Underlying foundation error: \(error)."
  515. case .cannotSetCacheFileAttribute(let filePath, let attributes, let error):
  516. return "Cannot set file attribute for the cache file at path: \(filePath), attributes: \(attributes)." +
  517. "Underlying foundation error: \(error)."
  518. case .diskStorageIsNotReady(let cacheURL):
  519. return "The disk storage is not ready to use yet at URL: '\(cacheURL)'. " +
  520. "This is usually caused by extremely lack of disk space. Ask users to free up some space and restart the app."
  521. case .missingLivePhotoResourceOnDisk(let resource):
  522. return "The live photo resource '\(resource)' is missing in the cache. Usually a re-download" +
  523. " can fix this issue."
  524. }
  525. }
  526. var errorCode: Int {
  527. switch self {
  528. case .fileEnumeratorCreationFailed: return 3001
  529. case .invalidFileEnumeratorContent: return 3002
  530. case .invalidURLResource: return 3003
  531. case .cannotLoadDataFromDisk: return 3004
  532. case .cannotCreateDirectory: return 3005
  533. case .imageNotExisting: return 3006
  534. case .cannotConvertToData: return 3007
  535. case .cannotSerializeImage: return 3008
  536. case .cannotCreateCacheFile: return 3009
  537. case .cannotSetCacheFileAttribute: return 3010
  538. case .diskStorageIsNotReady: return 3011
  539. case .missingLivePhotoResourceOnDisk: return 3012
  540. }
  541. }
  542. }
  543. extension KingfisherError.ProcessorErrorReason {
  544. var errorDescription: String? {
  545. switch self {
  546. case .processingFailed(let processor, let item):
  547. return "Processing image failed. Processor: \(processor). Processing item: \(item)."
  548. }
  549. }
  550. var errorCode: Int {
  551. switch self {
  552. case .processingFailed: return 4001
  553. }
  554. }
  555. }
  556. extension KingfisherError.ImageSettingErrorReason {
  557. var errorDescription: String? {
  558. switch self {
  559. case .emptySource:
  560. return "The input resource is empty."
  561. case .notCurrentSourceTask(let result, let error, let resource):
  562. if let result = result {
  563. return "Retrieving resource succeeded, but this source is " +
  564. "not the one currently expected. Result: \(result). Resource: \(resource)."
  565. } else if let error = error {
  566. return "Retrieving resource failed, and this resource is " +
  567. "not the one currently expected. Error: \(error). Resource: \(resource)."
  568. } else {
  569. return nil
  570. }
  571. case .dataProviderError(let provider, let error):
  572. return "Image data provider fails to provide data. Provider: \(provider), error: \(error)"
  573. case .alternativeSourcesExhausted(let errors):
  574. return "Image setting from alternative sources failed: \(errors)"
  575. case .notCurrentLivePhotoSourceTask(let result, let error, let source):
  576. if let result = result {
  577. return "Retrieving live photo resource succeeded, but this source is " +
  578. "not the one currently expected. Result: \(result). Resource: \(source)."
  579. } else if let error = error {
  580. return "Retrieving live photo resource failed, and this resource is " +
  581. "not the one currently expected. Error: \(error). Resource: \(source)."
  582. } else {
  583. return nil
  584. }
  585. case .livePhotoResultError(let result, let error, let source):
  586. return "An error occurred while processing live photo. Source: \(source). " +
  587. "Result: \(String(describing: result)). Error: \(String(describing: error))"
  588. }
  589. }
  590. var errorCode: Int {
  591. switch self {
  592. case .emptySource: return 5001
  593. case .notCurrentSourceTask: return 5002
  594. case .dataProviderError: return 5003
  595. case .alternativeSourcesExhausted: return 5004
  596. case .notCurrentLivePhotoSourceTask: return 5005
  597. case .livePhotoResultError: return 5006
  598. }
  599. }
  600. }