0

I'm trying to do a retrywhen on a http.get for X-times when communication fails.

After the X-times I would like to throw an error that it failed to connect.

If I only do in my DelayWhen an Observable.throw it directly works fine and returns on the first try. But if i for the first times return an Obersvable.timer, to delay the next to tries for connection afterwards the Observable.throw is called but it will not be forwarded to my upper level catch!?

public initializeCommunication(): Promise<boolean> {
 return new Promise<boolean>((resolve: (r: boolean) => void, reject: (e: 
  Error) => void) => {
 return this.getInternal<DTO>(this.Url, null, 
 'clientidentity')
.retryWhen((errors: Observable<any>) => {
                    // Retry upon communication failure
                    // TODO: Consider only retrying a limited number of 
                    //times and throw an error (call reject)

                    return errors.delayWhen((response: Response) => {
                        retrytrys++;
                        console.log('number of retries   ' + retrytrys);

                        if (retrytrys > 2) {
                            console.log('throw');
                            return Observable.throw(new Error('error!'));
                        }
                        console.log('ret');
                        return Observable.timer(this.RETRY_TIMEOUT);
                    });
                })
                .subscribe()
});
}

and this function I'm calling from another service and in that the catch is not called.

            myService.initializeCommunication().then((r: boolean) => {

            })
            .catch((error) => { //this is not called }
MARL
  • 41
  • 1
  • 4
  • Your code works fine. What do you mean about 'upper level catch'? Can you update example with it? – Aleksandr Petrovskij Sep 13 '17 at 14:21
  • I edit the code snipped with more details. I mean that the catch of my other service is not called even that an error is throw in the `retrywhen`. If i don't return the `Observable.timer` but directly on first run trough the error than the error will be catched! – MARL Sep 15 '17 at 14:14
  • Here is the [plnkr](https://plnkr.co/edit/VYZhPYoKnLwZcalZHxsf?p=preview) where everything works – Aleksandr Petrovskij Sep 16 '17 at 11:02
  • thanks very much it works, yes. – MARL Sep 18 '17 at 06:51

0 Answers0