Using `retryWhen` in http requests

≡放荡痞女 提交于 2019-12-12 16:34:32

问题


I have extended Http class to set default headers and handle errors for all the requests inside an angular2 app, and I would like to use retryWhen() to retry the requests on certain errors.

On which errors should retryWhen retry the request and on which errors should it stop?

@Injectable()
export class WpHttp extends Http {

  constructor(backend: ConnectionBackend,
            defaultOptions: RequestOptions,
            private wpService: WpService) {
    super(backend, defaultOptions);
  }

   get(endpoint: string, args?: QueryArgs): Observable<Response> {

    return super.get(this.getUrl(endpoint, args), this.getOptions())

        /** retry the request after 1 second */
        .retryWhen(error => error.delay(1000))

        /** timeout 3s
        .timeout(3000 , new Error('delay exceeded'))

        .catch((err) => {
            if (err.status === 400 || err.status === 422) {
                return Observable.throw(err);
            } 
            else {
                /** Stream errors in WpService */
                this.wpService.errors.next(err);
            }
        })
        .finally(() => {

        });
   }
}

And should it be used for post, put, update, delete requests?

来源:https://stackoverflow.com/questions/39189111/using-retrywhen-in-http-requests

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