Angular 2 RxJS Observable: Retry except on 429 status

試著忘記壹切 提交于 2019-12-11 00:33:41

问题


I've composed my Observable (from an HTTP request) to retry on failure. However, I would like to not retry if the server responded with 429 Too many requests error.

The current implementation retries twice, 1 second apart, no matter what.

return this.http.get(url,options)
    .retryWhen(errors => {
        return errors.delay(1000).take(2);
    })
    .catch((res)=>this.handleError(res));

errors is an Observable. How can I get the underlying Response object that caused the error? With it I can access the server's status code and only retry if it's not 429:

return this.http.get(url,options)
    .retryWhen(errors => {
        if($code == 429) throw errors;
        else return errors.delay(1000).take(2);
    })
.catch((res)=>this.handleError(res));

How can I get status code within retryWhen?

Live demo on Plunker

Angular 2 rc.6, RxJS 5 Beta 11, Typescript 2.0.2


回答1:


You can compose the handling of 429 errors into the errors observable that's passed to retryWhen. The errors that are emitted from the errors observable will contain a status property if they are errors that were received from the server.

If you don't want to retry when 429 errors occur and instead wish to throw an error you could do something like this:

return this.http.get(url,options)
    .retryWhen((errors) => {
        return errors
            .mergeMap((error) => (error.status === 429) ? Observable.throw(error) : Observable.of(error))
            .delay(1000)
            .take(2);
    })
    .catch((res) => this.handleError(res));

If, instead, you wanted the HTTP observable to complete without emitting either an error or a response, you could simply filter 429 errors.



来源:https://stackoverflow.com/questions/39480348/angular-2-rxjs-observable-retry-except-on-429-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!