Angular 2 RxJS Observed: Repeat, except for status 429

I wrote my Observable (from an HTTP request) to try again. However, I would not repeat if the server responded with a 429 Too many requests error.

The current implementation is repeated twice, for 1 second, no matter what.

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

errors is observable. How can I get the base Response object that caused the error? With it, I can access the server status code and only try again if it is 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 the status code inside retryWhen ?

Live demo on Plunker

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

+1
angular typescript observable rxjs5
source share
1 answer

You can compile handling 429 errors in the observed errors , which is passed to retryWhen . Errors that are emitted from the observed errors will contain the status property if these are errors received from the server.

If you do not want to try again when 429 errors occur, and instead you want to make a mistake, you can 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 want the HTTP observable to complete without an error or response, you could just filter 429 errors.

+9
source share

All Articles