Reputation: 39
I'm migrating from RxSwift to Combine and ran into this issue related to error handling:
func myMethod(firstName: String?) -> Observable<Data> {
guard let firstName = firstName else {
return Observable.error(MyErrorEnum.error)
}
return someAPICall...
}
I'm migrating to Combine as follows:
func myMethod(firstName: String?) -> AnyPublisher<Data, Error> {
guard let firstName = firstName else {
// can't quite figure out how to return an error here...
}
return someAPICall...
}
I normally don't struggle handling errors, specially when working with network calls, but for some reason this one is avoiding me.
Really appreciate any input!
Upvotes: 1
Views: 850
Reputation: 32780
You can use the Fail
publisher:
func myMethod(firstName: String?) -> AnyPublisher<Data, Error> {
guard let firstName = firstName else {
return Fail<Data, Error>(error: MyErrorEnum.error).eraseToAnyPublisher()
}
return someAPICall...
}
But I'd argue that your design is the optimal one, you'd better simply require a non-optional firstName
and make the callers aware that your method doesn't work well with nil values.
Upvotes: 1