Swift Combine - 如何抛出错误并停止执行

Swift Combine - How to throw an error and stop execution

我有一个 HTTP 请求发布者,当返回 401 错误时,我想停止执行并显示我的登录屏幕。

这是我的部分代码:

cancellable = fetcher.hello(helloRequest: HelloRequest(name: self.name))
            .print("fetcher.hello")
            .catch { _ in
                // TODO: how to handle errors with request?
                Just(HelloResponse.placeHolder)
            }
            .flatMap { response -> AnyPublisher<HelloResponse, Never> in
                if response.imageUrl == nil || response.imageUrl == "" {
                    // If there's no image to download just return the response
                    return Just(response).eraseToAnyPublisher()
                }
                else {
                    // Chain together request and download image
                    return fetcher.downloadImage(url: response.imageUrl!)
                        .print("fetcher.hello.downloadImage")
                        .catch { _ in
                            // If there was an error downloading the image, replace it with a placeholder
                            Just(UIImage(named: "placeholder_square")!)
                        }
                        .map {
                            // Add image to response
                            HelloResponse(message: response.message, visitCount: response.visitCount, imageUrl: response.imageUrl, image: [=11=])
                        }
                        .eraseToAnyPublisher()
                }
            }
            .sink(receiveCompletion: { _ in }, receiveValue: { self.response = [=11=].self })

所以部分问题是以下 flatMap 会在必要时下载图像。输出类型是 AnyPublisher (我想不出另一种方法来做到这一点)。现在 catch returns 占位符模型并且工作正常。但现在我已经接受了这个错误。我认为 Empty() 发布者可能会工作,但它似乎不正确。我试过 Fail() 但显然 catch 是 Never (有道理)。谢谢!

使用 New Dev 关于 setFailureType 的建议,我现在可以使用它了。当我最初使用 flatMap returning AnyPublisher 时,它不会编译,因为 Just 是 Never。所以我将 flatMap 更改为 Never fail 但后来我无法删除 catch,因为之前的失败类型是 Error。现在将 setFailureType 添加到 flatMap 中的 Just 让我将 flatMap 更改为 return 错误,然后我删除了捕获。

cancellable = fetcher.hello(helloRequest: HelloRequest(name: self.name))
            .print("fetcher.hello")
            .flatMap { response -> AnyPublisher<HelloResponse, Error> in
                if response.imageUrl == nil || response.imageUrl == "" {
                    // If there's no image to download just return the response
                    return Just(response)
                        .setFailureType(to: Error.self) // This allows us to set a failure type (Just is Never) so that it will match AnyPublisher<HelloResponse, Error>
                        .eraseToAnyPublisher()
                }
                else {
                    // Chain together request and download image
                    return fetcher.downloadImage(url: response.imageUrl!)
                        .print("fetcher.hello.downloadImage")
                        .catch { _ in
                            // If there was an error downloading the image, replace it with a placeholder
                            Just(UIImage(named: "placeholder_square")!)
                                .setFailureType(to: Error.self) // This allows us to set a failure type (Just is Never) so that it will match AnyPublisher<HelloResponse, Error>
                        }
                        .map {
                            // Add image to response
                            HelloResponse(message: response.message, visitCount: response.visitCount, imageUrl: response.imageUrl, image: [=10=])
                        }
                        .eraseToAnyPublisher()
                }
            }
            .sink(receiveCompletion: { completion in
                switch completion {
                case .finished:
                    break
                case .failure(let error):
                    print("Request error: \(String(describing: error))")
                }
            }, receiveValue: {
                self.response = [=10=].self
            })
    }