Using retryWhen to update tokens based on http error code

前端 未结 3 1021
我寻月下人不归
我寻月下人不归 2020-12-31 02:55

I found this example on How to refresh oauth token using moya and rxswift which I had to alter slightly to get to compile. This code works 80% for my scenario. The problem w

3条回答
  •  长发绾君心
    2020-12-31 03:37

    Compilation Error

    Which line has the compilation error? It seems to me that it would be this line:

    .catchError {
        error in
        //...
        return Observable.error(error)  // is this the line causing the compilation error?
    }
    

    If so, it's probably because catchError is expecting the block to return an Observable with which it can continue in case of an error, and not an Observable.

    In either case, it helps to annotate your code with more types so that you can pinpoint problems like this, as well as help the Swift compiler, which often can't figure out these kinds of things on its own. So something like this would have helped you:

    .catchError {
        error -> Observable in
        //...
        return Observable.error(error)  // Swift should have a more accurate and helpful error message here now
    }
    

    Note that I'm only showing you what the error is and how to get Xcode to give you better error messages. What you're trying to return still isn't correct.

    Only retry on 401

    I'm not sure why you're expecting this code to treat 401 differently (other than posting to the notification center and logging). As it is, you're catching the error, but you're always returning an Observable with an Error event at the end (return Observable.error(error)), so it will never retry.

    To get 401 to retry, you should return an Observable from the retryWhen block, which will send a Next event (signifying that you want to retry). For all other status codes, that Observable should send an Error (as you're currently doing), which will signify that you don't want to retry, and that you'd like the error propagated.

    So something like this:

    .retryWhen { errorObservable -> Observable in
        log.debug("ReAuth error: \(error)")
        if case Error.StatusCode(let response) = error where response.statusCode == 401 {
            log.debug("401:, force user logout")
            NSNotificationCenter.defaultCenter().postNotificationName(Constants.Notifications.userNotAuthenticated, object: nil, userInfo: nil)
            // If `401`, then return the `Observable` which was given to us
            // It will emit a `.Next`
            // Since it is a `.Next` event, `retryWhen` will retry.
            return errorObservable
        }
        else {
            // If not `401`, then `flatMap` the `Observable` which
            // is about to emit a `.Next` into
            // an `Observable` which will instead emit a `.Error`.
            // Since it is an `.Error` event, `retryWhen` will *not* retry.
            // Instead, it will propagate the error.
            return errorObservable.flatMap { Observable.error($0) }
        }
    }
    

提交回复
热议问题