Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 RxJS Observable: Retry except on 429 status

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

like image 219
BeetleJuice Avatar asked Dec 14 '22 03:12

BeetleJuice


1 Answers

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.

like image 94
cartant Avatar answered Dec 16 '22 17:12

cartant