فهرست منبع

Make Result error type constrained

onevcat 7 سال پیش
والد
کامیت
7ede63cb12

+ 1 - 1
Demo/Demo/Kingfisher-macOS-Demo/ViewController.swift

@@ -62,7 +62,7 @@ extension ViewController: NSCollectionViewDataSource {
                                                    progressBlock: { receivedSize, totalSize in
                                                     print("\(indexPath.item + 1): \(receivedSize)/\(totalSize)")
                                                     },
-                                              completionHandler: { image, error, cacheType, imageURL in
+                                              completionHandler: { result in
                                                     print("\(indexPath.item + 1): Finished")
                                                     })
         

+ 16 - 7
Sources/Cache/ImageCache.swift

@@ -62,8 +62,8 @@ public enum CacheType {
 }
 
 public struct CacheStoreResult {
-    let memoryCacheResult: Result<()>
-    let diskCacheResult: Result<()>
+    let memoryCacheResult: Result<(), Never>
+    let diskCacheResult: Result<(), KingfisherError>
 }
 
 extension Image: CacheCostCalculatable {
@@ -290,7 +290,7 @@ open class ImageCache {
     open func retrieveImage(forKey key: String,
                                options: KingfisherOptionsInfo? = nil,
                         callbackQueue: CallbackQueue = .untouch,
-                     completionHandler: ((Result<(ImageCacheResult)>) -> Void)?)
+                     completionHandler: ((Result<ImageCacheResult, KingfisherError>) -> Void)?)
     {
         // No completion handler. Not start working and early return.
         guard let completionHandler = completionHandler else { return }
@@ -361,7 +361,7 @@ open class ImageCache {
         forKey key: String,
         options: KingfisherOptionsInfo? = nil,
         callbackQueue: CallbackQueue = .untouch,
-        completionHandler: @escaping (Result<Image?>) -> Void)
+        completionHandler: @escaping (Result<Image?, KingfisherError>) -> Void)
     {
         let options = options ?? .empty
         let computedKey = key.computedKey(with: options.processor.identifier)
@@ -373,7 +373,11 @@ open class ImageCache {
                 }
                 callbackQueue.execute { completionHandler(.success(image)) }
             } catch {
-                callbackQueue.execute { completionHandler(.failure(error)) }
+                if let error = error as? KingfisherError {
+                    callbackQueue.execute { completionHandler(.failure(error)) }
+                } else {
+                    assertionFailure("The internal thrown error should be a `KingfisherError`.")
+                }
             }
         }
     }
@@ -498,7 +502,7 @@ open class ImageCache {
     
     - parameter completionHandler: Called with the calculated size when finishes.
     */
-    open func calculateDiskCacheSize(completion handler: @escaping ((Result<UInt>) -> Void)) {
+    open func calculateDiskCacheSize(completion handler: @escaping ((Result<UInt, KingfisherError>) -> Void)) {
         ioQueue.async {
             do {
                 let size = try self.diskStorage.totalSize()
@@ -506,7 +510,12 @@ open class ImageCache {
                     handler(.success(size))
                 }
             } catch {
-                handler(.failure(error))
+                if let error = error as? KingfisherError {
+                    handler(.failure(error))
+                } else {
+                    assertionFailure("The internal thrown error should be a `KingfisherError`.")
+                }
+                
             }
         }
     }

+ 3 - 2
Sources/Extensions/ImageView+Kingfisher.swift

@@ -53,7 +53,8 @@ extension KingfisherClass where Base: ImageView {
                          placeholder: Placeholder? = nil,
                          options: KingfisherOptionsInfo? = nil,
                          progressBlock: DownloadProgressBlock? = nil,
-                         completionHandler: ((Result<RetrieveImageResult>) -> Void)? = nil) -> DownloadTask?
+                         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil)
+        -> DownloadTask?
     {
         guard let resource = resource else {
             self.placeholder = placeholder
@@ -93,7 +94,7 @@ extension KingfisherClass where Base: ImageView {
                     maybeIndicator?.stopAnimatingView()
                     guard resource.downloadURL == self.webURL else {
                         let error = KingfisherError.imageSettingError(
-                            reason: .notCurrentResource(result: result, resource: resource))
+                            reason: .notCurrentResource(result: result.value, error: result.error, resource: resource))
                         completionHandler?(.failure(error))
                         return
                     }

+ 6 - 4
Sources/Extensions/NSButton+Kingfisher.swift

@@ -50,7 +50,8 @@ extension KingfisherClass where Base: NSButton {
                          placeholder: Image? = nil,
                          options: KingfisherOptionsInfo? = nil,
                          progressBlock: DownloadProgressBlock? = nil,
-                         completionHandler: ((Result<RetrieveImageResult>) -> Void)? = nil) -> DownloadTask?
+                         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil)
+        -> DownloadTask?
     {
         guard let resource = resource else {
             base.image = placeholder
@@ -76,7 +77,7 @@ extension KingfisherClass where Base: NSButton {
                 DispatchQueue.main.safeAsync {
                     guard resource.downloadURL == self.webURL else {
                         let error = KingfisherError.imageSettingError(
-                            reason: .notCurrentResource(result: result, resource: resource))
+                            reason: .notCurrentResource(result: result.value, error: result.error, resource: resource))
                         completionHandler?(.failure(error))
                         return
                     }
@@ -127,7 +128,8 @@ extension KingfisherClass where Base: NSButton {
                                   placeholder: Image? = nil,
                                   options: KingfisherOptionsInfo? = nil,
                                   progressBlock: DownloadProgressBlock? = nil,
-                                  completionHandler: ((Result<RetrieveImageResult>) -> Void)? = nil) -> DownloadTask?
+                                  completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil)
+        -> DownloadTask?
     {
         guard let resource = resource else {
             base.alternateImage = placeholder
@@ -153,7 +155,7 @@ extension KingfisherClass where Base: NSButton {
                 DispatchQueue.main.safeAsync {
                     guard resource.downloadURL == self.alternateWebURL else {
                         let error = KingfisherError.imageSettingError(
-                            reason: .notCurrentResource(result: result, resource: resource))
+                            reason: .notCurrentResource(result: result.value, error: result.error, resource: resource))
                         completionHandler?(.failure(error))
                         return
                     }

+ 6 - 4
Sources/Extensions/UIButton+Kingfisher.swift

@@ -56,7 +56,8 @@ extension KingfisherClass where Base: UIButton {
                          placeholder: UIImage? = nil,
                          options: KingfisherOptionsInfo? = nil,
                          progressBlock: DownloadProgressBlock? = nil,
-                         completionHandler: ((Result<RetrieveImageResult>) -> Void)? = nil) -> DownloadTask?
+                         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil)
+        -> DownloadTask?
     {
         guard let resource = resource else {
             base.setImage(placeholder, for: state)
@@ -82,7 +83,7 @@ extension KingfisherClass where Base: UIButton {
                 DispatchQueue.main.safeAsync {
                     guard resource.downloadURL == self.webURL(for: state) else {
                         let error = KingfisherError.imageSettingError(
-                            reason: .notCurrentResource(result: result, resource: resource))
+                            reason: .notCurrentResource(result: result.value, error: result.error, resource: resource))
                         completionHandler?(.failure(error))
                         return
                     }
@@ -136,7 +137,8 @@ extension KingfisherClass where Base: UIButton {
                                    placeholder: UIImage? = nil,
                                    options: KingfisherOptionsInfo? = nil,
                                    progressBlock: DownloadProgressBlock? = nil,
-                                   completionHandler: ((Result<RetrieveImageResult>) -> Void)? = nil) -> DownloadTask?
+                                   completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil)
+        -> DownloadTask?
     {
         guard let resource = resource else {
             base.setBackgroundImage(placeholder, for: state)
@@ -166,7 +168,7 @@ extension KingfisherClass where Base: UIButton {
                 DispatchQueue.main.safeAsync {
                     guard resource.downloadURL == self.backgroundWebURL(for: state) else {
                         let error = KingfisherError.imageSettingError(
-                            reason: .notCurrentResource(result: result, resource: resource))
+                            reason: .notCurrentResource(result: result.value, error: result.error, resource: resource))
                         completionHandler?(.failure(error))
                         return
                     }

+ 3 - 2
Sources/Extensions/WKInterfaceImage+Kingfisher.swift

@@ -46,7 +46,8 @@ extension KingfisherClass where Base: WKInterfaceImage {
                          placeholder: Image? = nil,
                          options: KingfisherOptionsInfo? = nil,
                          progressBlock: DownloadProgressBlock? = nil,
-                         completionHandler: ((Result<RetrieveImageResult>) -> Void)? = nil) -> DownloadTask?
+                         completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil)
+        -> DownloadTask?
     {
         guard let resource = resource else {
             base.setImage(placeholder)
@@ -72,7 +73,7 @@ extension KingfisherClass where Base: WKInterfaceImage {
                 DispatchQueue.main.safeAsync {
                     guard resource.downloadURL == self.webURL else {
                         let error = KingfisherError.imageSettingError(
-                            reason: .notCurrentResource(result: result, resource: resource))
+                            reason: .notCurrentResource(result: result.value, error: result.error, resource: resource))
                         completionHandler?(.failure(error))
                         return
                     }

+ 4 - 2
Sources/General/KingfisherError.swift

@@ -29,6 +29,8 @@ import Foundation
 /// Error domain of Kingfisher
 public let KingfisherErrorDomain = "com.onevcat.Kingfisher.Error"
 
+extension Never: Error {}
+
 public enum KingfisherError: Error {
     
     public enum RequestErrorReason {
@@ -62,7 +64,7 @@ public enum KingfisherError: Error {
 
     public enum ImageSettingErrorReason {
         case emptyResource
-        case notCurrentResource(result: Result<RetrieveImageResult>, resource: Resource)
+        case notCurrentResource(result: RetrieveImageResult?, error: Error?, resource: Resource)
     }
     
     case requestError(reason: RequestErrorReason)
@@ -72,7 +74,7 @@ public enum KingfisherError: Error {
     case imageSettingError(reason: ImageSettingErrorReason)
     
     var isTaskCancelled: Bool {
-        if case KingfisherError.requestError(reason: .taskCancelled) = self {
+        if case .requestError(reason: .taskCancelled) = self {
             return true
         }
         return false

+ 3 - 3
Sources/General/KingfisherManager.swift

@@ -114,7 +114,7 @@ public class KingfisherManager {
         with resource: Resource,
         options: KingfisherOptionsInfo? = nil,
         progressBlock: DownloadProgressBlock? = nil,
-        completionHandler: ((Result<RetrieveImageResult>) -> Void)?) -> DownloadTask?
+        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
     {
         let options = currentDefaultOptions + (options ?? .empty)
         if options.forceRefresh {
@@ -168,7 +168,7 @@ public class KingfisherManager {
         forKey key: String,
         options: KingfisherOptionsInfo,
         progressBlock: DownloadProgressBlock? = nil,
-        completionHandler: ((Result<RetrieveImageResult>) -> Void)?) -> DownloadTask?
+        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
     {
         let downloader = options.downloader ?? self.downloader
 
@@ -251,7 +251,7 @@ public class KingfisherManager {
         forKey key: String,
         with url: URL,
         options: KingfisherOptionsInfo,
-        completionHandler: ((Result<RetrieveImageResult>) -> Void)?) -> Bool
+        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> Bool
     {
         // 1. Check whether the image was already in target cache. If so, just get it.
         let targetCache = options.targetCache ?? cache

+ 2 - 2
Sources/Networking/ImageDataProcessor.swift

@@ -33,7 +33,7 @@ class ImageDataProcessor {
 
     // Note: We have an optimization choice there, to reduce queue dispatch by checking callback
     // queue settings in each option...
-    let onImageProcessed = Delegate<(Result<Image>, SessionDataTask.TaskCallback), Void>()
+    let onImageProcessed = Delegate<(Result<Image, KingfisherError>, SessionDataTask.TaskCallback), Void>()
 
     private let processQueue: DispatchQueue
 
@@ -57,7 +57,7 @@ class ImageDataProcessor {
                 processedImages[processor.identifier] = image
             }
 
-            let result: Result<Image>
+            let result: Result<Image, KingfisherError>
             if let image = image {
                 let imageModifier = callback.options.imageModifier
                 var finalImage = imageModifier.modify(image)

+ 4 - 3
Sources/Networking/ImageDownloader.swift

@@ -178,7 +178,8 @@ open class ImageDownloader {
     open func downloadImage(with url: URL,
                             options: KingfisherOptionsInfo? = nil,
                             progressBlock: DownloadProgressBlock? = nil,
-                            completionHandler: ((Result<ImageDownloadResult>) -> Void)? = nil) -> DownloadTask?
+                            completionHandler: ((Result<ImageDownloadResult, KingfisherError>) -> Void)? = nil)
+        -> DownloadTask?
     {
         // Creates default request.
         var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: downloadTimeout)
@@ -216,8 +217,8 @@ open class ImageDownloader {
         }
 
         let onCompleted = completionHandler.map {
-            block -> Delegate<Result<ImageDownloadResult>, Void> in
-            let delegate =  Delegate<Result<ImageDownloadResult>, Void>()
+            block -> Delegate<Result<ImageDownloadResult, KingfisherError>, Void> in
+            let delegate =  Delegate<Result<ImageDownloadResult, KingfisherError>, Void>()
             delegate.delegate(on: self) { (_, result) in
                 block(result)
             }

+ 1 - 1
Sources/Networking/ImagePrefetcher.swift

@@ -199,7 +199,7 @@ public class ImagePrefetcher {
     
     func downloadAndCache(_ resource: Resource) {
 
-        let downloadTaskCompletionHandler: ((Result<RetrieveImageResult>) -> Void) = { result in
+        let downloadTaskCompletionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void) = { result in
             self.tasks.removeValue(forKey: resource.downloadURL)
             if let _ = result.error {
                 self.failedResources.append(resource)

+ 2 - 2
Sources/Networking/SessionDataTask.swift

@@ -35,7 +35,7 @@ public class SessionDataTask {
 
     struct TaskCallback {
         let onProgress: Delegate<(Int64, Int64), Void>?
-        let onCompleted: Delegate<Result<ImageDownloadResult>, Void>?
+        let onCompleted: Delegate<Result<ImageDownloadResult, KingfisherError>, Void>?
         let options: KingfisherOptionsInfo
     }
 
@@ -53,7 +53,7 @@ public class SessionDataTask {
     private var currentToken = 0
     private let lock = NSLock()
 
-    let onTaskDone = Delegate<(Result<(Data, URLResponse?)>, [TaskCallback]), Void>()
+    let onTaskDone = Delegate<(Result<(Data, URLResponse?), KingfisherError>, [TaskCallback]), Void>()
     let onCallbackCancelled = Delegate<(CancelToken, TaskCallback), Void>()
 
     var started = false

+ 4 - 4
Sources/Networking/SessionDelegate.swift

@@ -46,7 +46,7 @@ class SessionDelegate: NSObject {
     private let lock = NSLock()
 
     let onValidStatusCode = Delegate<Int, Bool>()
-    let onDownloadingFinished = Delegate<(URL, Result<URLResponse>), Void>()
+    let onDownloadingFinished = Delegate<(URL, Result<URLResponse, KingfisherError>), Void>()
     let onDidDownloadData = Delegate<SessionDataTask, Data?>()
 
     let onReceiveSessionChallenge = Delegate<SessionChallengeFunc, Void>()
@@ -173,7 +173,7 @@ extension SessionDelegate: URLSessionDataDelegate {
         guard let sessionTask = self.task(for: task) else { return }
 
         if let url = task.originalRequest?.url {
-            let result: Result<(URLResponse)>
+            let result: Result<URLResponse, KingfisherError>
             if let error = error {
                 result = .failure(KingfisherError.responseError(reason: .URLSessionError(error: error)))
             } else if let response = task.response {
@@ -184,7 +184,7 @@ extension SessionDelegate: URLSessionDataDelegate {
             onDownloadingFinished.call((url, result))
         }
 
-        let result: Result<(Data, URLResponse?)>
+        let result: Result<(Data, URLResponse?), KingfisherError>
         if let error = error {
             result = .failure(KingfisherError.responseError(reason: .URLSessionError(error: error)))
         } else {
@@ -214,7 +214,7 @@ extension SessionDelegate: URLSessionDataDelegate {
         onReceiveSessionTaskChallenge.call((session, task, challenge, completionHandler))
     }
 
-    private func onCompleted(task: URLSessionTask, result: Result<(Data, URLResponse?)>) {
+    private func onCompleted(task: URLSessionTask, result: Result<(Data, URLResponse?), KingfisherError>) {
         guard let sessionTask = self.task(for: task) else {
             return
         }

+ 2 - 2
Sources/Utility/Result.swift

@@ -30,7 +30,7 @@ import Foundation
 ///
 /// - success: The operation is successful and an associated value could be provided.
 /// - failure: An error happens during the operation.
-public enum Result<Value> {
+public enum Result<Value, Error: Swift.Error> {
     case success(Value)
     case failure(Error)
     
@@ -70,7 +70,7 @@ public enum Result<Value> {
     /// - Parameter transform: A closure that takes the success value of the instance.
     /// - Returns: A `Result` containing the result of the given closure. If this instance is a failure, returns the
     ///            same failure.
-    public func map<T>(_ transform: (Value) -> T) -> Result<T> {
+    public func map<T>(_ transform: (Value) -> T) -> Result<T, Error> {
         switch self {
         case .success(let value): return .success(transform(value))
         case .failure(let error): return .failure(error)

+ 10 - 10
Tests/KingfisherTests/ImageDownloaderTests.swift

@@ -129,7 +129,7 @@ class ImageDownloaderTests: XCTestCase {
         let someURL = URL(string: "some_strange_url")!
         downloader.downloadImage(with: someURL, options: [.requestModifier(nilModifier)]) { result in
             XCTAssertNotNil(result.error)
-            guard case KingfisherError.requestError(reason: .emptyRequest) = result.error! else {
+            guard case .requestError(reason: .emptyRequest) = result.error! else {
                 XCTFail()
                 fatalError()
             }
@@ -146,7 +146,7 @@ class ImageDownloaderTests: XCTestCase {
         
         downloader.downloadImage(with: url) { result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isInvalidResponseStatusCode(404))
+            XCTAssertTrue(result.error!.isInvalidResponseStatusCode(404))
             exp.fulfill()
         }
         
@@ -165,7 +165,7 @@ class ImageDownloaderTests: XCTestCase {
         
         downloader.downloadImage(with: url) { result in
             XCTAssertNotNil(result.error)
-            if case KingfisherError.responseError(reason: .URLSessionError(let error)) = result.error! {
+            if case .responseError(reason: .URLSessionError(let error)) = result.error! {
                 let nsError = error as NSError
                 XCTAssert(nsError.code == NSURLErrorServerCertificateUntrusted ||
                           nsError.code == NSURLErrorSecureConnectionFailed,
@@ -219,7 +219,7 @@ class ImageDownloaderTests: XCTestCase {
         {
             result in
             XCTAssertNotNil(result.error)
-            if case KingfisherError.requestError(reason: .invalidURL(let request)) = result.error! {
+            if case .requestError(reason: .invalidURL(let request)) = result.error! {
                 XCTAssertNil(request.url)
             } else {
                 XCTFail()
@@ -246,7 +246,7 @@ class ImageDownloaderTests: XCTestCase {
         {
             result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+            XCTAssertTrue(result.error!.isTaskCancelled)
             delay(0.1) { exp.fulfill() }
         }
         
@@ -300,7 +300,7 @@ class ImageDownloaderTests: XCTestCase {
             group.enter()
             downloader.downloadImage(with: $0) { result in
                 XCTAssertNotNil(result.error)
-                XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+                XCTAssertTrue(result.error!.isTaskCancelled)
                 group.leave()
             }
         }
@@ -330,14 +330,14 @@ class ImageDownloaderTests: XCTestCase {
         group.enter()
         downloader.downloadImage(with: url1) { result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+            XCTAssertTrue(result.error!.isTaskCancelled)
             group.leave()
         }
         
         group.enter()
         downloader.downloadImage(with: url1) { result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+            XCTAssertTrue(result.error!.isTaskCancelled)
             group.leave()
         }
         
@@ -375,7 +375,7 @@ class ImageDownloaderTests: XCTestCase {
         {
             result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+            XCTAssertTrue(result.error!.isTaskCancelled)
             group.leave()
         }
         
@@ -469,7 +469,7 @@ class ImageDownloaderTests: XCTestCase {
         downloader.downloadImage(with: url) { result in
             XCTAssertNil(result.value)
             XCTAssertNotNil(result.error)
-            if case KingfisherError.responseError(reason: .dataModifyingFailed) = result.error! {
+            if case .responseError(reason: .dataModifyingFailed) = result.error! {
             } else {
                 XCTFail()
             }

+ 5 - 5
Tests/KingfisherTests/ImageViewExtensionTests.swift

@@ -341,7 +341,7 @@ class ImageViewExtensionTests: XCTestCase {
 
         imageView.kf.setImage(with: url, progressBlock: { _, _ in XCTFail() }) { result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+            XCTAssertTrue(result.error!.isTaskCancelled)
             delay(0.1) { exp.fulfill() }
         }
 
@@ -363,11 +363,11 @@ class ImageViewExtensionTests: XCTestCase {
         imageView.kf.setImage(with: testURLs[0]) { result in
             // The download successed, but not the resource we want.
             XCTAssertNotNil(result.error)
-            if case KingfisherError.imageSettingError(
-                reason: .notCurrentResource(let result, let resource)) = result.error!
+            if case .imageSettingError(
+                reason: .notCurrentResource(let result, _, let resource)) = result.error!
             {
                 XCTAssertEqual(resource.downloadURL, testURLs[0])
-                XCTAssertNotEqual(result.value!.image, self.imageView.image)
+                XCTAssertNotEqual(result!.image, self.imageView.image)
             } else {
                 XCTFail()
             }
@@ -392,7 +392,7 @@ class ImageViewExtensionTests: XCTestCase {
         imageView.kf.setImage(with: url, progressBlock: { _, _ in XCTFail() }) {
             result in
             XCTAssertNotNil(result.error)
-            guard case KingfisherError.imageSettingError(reason: .emptyResource) = result.error! else {
+            guard case .imageSettingError(reason: .emptyResource) = result.error! else {
                 XCTFail()
                 fatalError()
             }

+ 3 - 3
Tests/KingfisherTests/KingfisherManagerTests.swift

@@ -201,7 +201,7 @@ class KingfisherManagerTests: XCTestCase {
         manager.retrieveImage(with: url, options: [.onlyFromCache]) { result in
             XCTAssertNil(result.value)
             XCTAssertNotNil(result.error)
-            if case KingfisherError.cacheError(reason: .imageNotExisting(let key)) = result.error! {
+            if case .cacheError(reason: .imageNotExisting(let key)) = result.error! {
                 XCTAssertEqual(key, url.cacheKey)
             } else {
                 XCTFail()
@@ -219,7 +219,7 @@ class KingfisherManagerTests: XCTestCase {
         manager.retrieveImage(with: url) { result in
             XCTAssertNotNil(result.error)
             XCTAssertTrue(Thread.isMainThread)
-            XCTAssertTrue((result.error as! KingfisherError).isInvalidResponseStatusCode(404))
+            XCTAssertTrue(result.error!.isInvalidResponseStatusCode(404))
             exp.fulfill()
         }
         waitForExpectations(timeout: 1, handler: nil)
@@ -377,7 +377,7 @@ class KingfisherManagerTests: XCTestCase {
             self.manager.retrieveImage(with: url, options: [.processor(p), .waitForCache]) { result in
                 XCTAssertNotNil(result.error)
                 XCTAssertTrue(p.processed)
-                if case KingfisherError.processorError(reason: .processingFailed(let processor, _)) = result.error! {
+                if case .processorError(reason: .processingFailed(let processor, _)) = result.error! {
                     XCTAssertEqual(processor.identifier, p.identifier)
                 } else {
                     XCTFail()

+ 3 - 3
Tests/KingfisherTests/NSButtonExtensionTests.swift

@@ -114,7 +114,7 @@ class NSButtonExtensionTests: XCTestCase {
         
         button.kf.setImage(with: url) { result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+            XCTAssertTrue(result.error!.isTaskCancelled)
             delay(0.1) { exp.fulfill() }
         }
         
@@ -131,7 +131,7 @@ class NSButtonExtensionTests: XCTestCase {
         
         button.kf.setAlternateImage(with: url) { result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+            XCTAssertTrue(result.error!.isTaskCancelled)
             delay(0.1) { exp.fulfill() }
         }
         
@@ -149,7 +149,7 @@ class NSButtonExtensionTests: XCTestCase {
             XCTAssertNil(result.value)
             XCTAssertNotNil(result.error)
             
-            guard case KingfisherError.imageSettingError(reason: .emptyResource) = result.error! else {
+            guard case .imageSettingError(reason: .emptyResource) = result.error! else {
                 XCTFail()
                 fatalError()
             }

+ 3 - 3
Tests/KingfisherTests/UIButtonExtensionTests.swift

@@ -118,7 +118,7 @@ class UIButtonExtensionTests: XCTestCase {
 
         button.kf.setImage(with: url, for: .highlighted) { result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+            XCTAssertTrue(result.error!.isTaskCancelled)
             delay(0.1) { exp.fulfill() }
         }
         
@@ -135,7 +135,7 @@ class UIButtonExtensionTests: XCTestCase {
         
         button.kf.setBackgroundImage(with: url, for: .highlighted) { result in
             XCTAssertNotNil(result.error)
-            XCTAssertTrue((result.error as! KingfisherError).isTaskCancelled)
+            XCTAssertTrue(result.error!.isTaskCancelled)
             delay(0.1) { exp.fulfill() }
         }
         
@@ -152,7 +152,7 @@ class UIButtonExtensionTests: XCTestCase {
         button.kf.setBackgroundImage(with: url, for: .normal) { result in
             XCTAssertNil(result.value)
             XCTAssertNotNil(result.error)
-            guard case KingfisherError.imageSettingError(reason: .emptyResource) = result.error! else {
+            guard case .imageSettingError(reason: .emptyResource) = result.error! else {
                 XCTFail()
                 return
             }