Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling Error #1

Open
WHuangz88 opened this issue Jan 26, 2022 · 9 comments
Open

Handling Error #1

WHuangz88 opened this issue Jan 26, 2022 · 9 comments

Comments

@WHuangz88
Copy link

Hi Nguyen, it's such a great example
But would you mind handling the error as well in the reducer when calling an API especially using Single instead Observeable

Thank you

@NguyenPhongVN
Copy link
Member

NguyenPhongVN commented Jan 26, 2022

Hi William Huang.
Thanks for your comment, you can catch error by using catchToEffect, it is like that.
Screenshot 2022-01-27 at 5 00 21 AM

@NguyenPhongVN
Copy link
Member

l don't know which project l used Observeable in, can you give me more information. I'm currently editing the project, if you have problem, let me know.
Thanks.

@WHuangz88
Copy link
Author

Screen Shot 2022-01-27 at 10 03 18

this it, cause lot of my project are using Single for the networking call, and I have tried using catchToEffect and it's not working
do you have any idea how?

@WHuangz88
Copy link
Author

Screen Shot 2022-01-27 at 10 07 13

for my current solution is I have to map my Single to Observeable and then wrap it using materialize so i can expect an Observable<Event<Element>> and then i flatMapLatest to get the .next | .error | .completed result

do you have a better solution (?)

@NguyenPhongVN
Copy link
Member

NguyenPhongVN commented Jan 27, 2022

Screen Shot 2022-01-27 at 10 03 18

this it, cause lot of my project are using Single for the networking call, and I have tried using catchToEffect and it's not working do you have any idea how?

actually, I also use Single in Networking

extension DataRequest: ReactiveCompatible {
  
}

extension Reactive where Base: DataRequest {
   @discardableResult
   public func response(queue: DispatchQueue = .main) -> Single<AFDataResponse<Data?>> {
     Single.create { [weak base] single in
       let cancellableToken = base?.response(queue: queue) { response in
         single(.success(response)
         )
       }
       return Disposables.create {
         cancellableToken?.cancel()
       }
     }
   }
}

because Effect is a wrapper of Observable and Single too, so we must convert to Observable to switch Effect and Single.
Screenshot 2022-01-27 at 12 55 01 PM
Screenshot 2022-01-27 at 12 55 39 PM

before you using eraseToEffect(), convert it to an Observable.

extension Single {
  public func catchToEffect() -> Effect<Result<Element, Error>> {
    self.asObservable().catchToEffect()
  }
}

@NguyenPhongVN
Copy link
Member

NguyenPhongVN commented Jan 27, 2022

#1 (comment)
your way is very cool,l have some suggestion:

public struct AnyError<Failure: Error>: Error {
  
  public var error: Failure
  
  public init(error: Failure) {
    self.error = error
  }
}

public extension Error {
  func asAnyError() -> AnyError<Self> {
    AnyError(error: self)
  }
}

extension AnyError: Equatable where Failure: Equatable {}

struct HttpError: Error, Equatable {
  var errorString: String
}

enum MainAction: Equtable {
 case responseTodo(Result<Data, AnyError<HttpError>>)
}

Screenshot 2022-01-27 at 1 05 55 PM

@NguyenPhongVN
Copy link
Member

I don't think it is better, do you have any idea?

@WHuangz88
Copy link
Author

Now I get it, and both way are actually good, depends on the expected output maybe 😀.

I think mine is more like that I want to show an error alert immediately rather than processing the response data, and so I could just focus on the error action of what the view should behave when an error is produced

mean while yours like you want to show both error and data to user (?)

@NguyenPhongVN
Copy link
Member

My backend maybe return the Error in Result.success, you can check in here.
https://todolistappproj.herokuapp.com/todos/
https://todolistappproj.herokuapp.com/todos/1
so l want check the error in my case .success to handle error. 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants