问题
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