BeetleJuice BeetleJuice - 1 year ago 455
TypeScript Question

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

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);

is an Observable. How can I get the underlying
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);

How can I get status code within

Live demo on Plunker

Angular 2 rc.6
RxJS 5 Beta 11
Typescript 2.0.2

Answer Source

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))
    .catch((res) => this.handleError(res));

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download