1

In angular I am new for httpClient and trying to implement a solution to retry a http call when the internet is disconnected ie error status = 0. When 3 failed attempts i would like it throw the original http response error.

Below is what I am trying but it doesn't work. Any ideas?

return this.httpClient.post('https://server.com/logins', data, {headers: headers})
      .pipe(retryWhen(errors => errors
        .pipe(map((err, index) => {          
          // Caught Error, throw immediately
          if (!err) {
            throw err;
          } 
          if (err.status !== 0) {
            throw err;
          }
          // Disconnection error
          if (index === 2) {
            throw err;
          }
          return err;
        }))
        .pipe(delay(1000)))) 

1 Answer 1

3

This solution should work if not please can you provide me a stackblitz and i will try to make it work

 return this.httpClient.post('https://server.com/logins', data, { headers: headers })
      .pipe(
        retryWhen(errors => errors
          .pipe(
            concatMap((error, count) => {
              if(count === 3 && error.status === 0) {
                  return throwError(error);
              } 
              return of(count); 
            }),
            delay(1000)
          )
        )
      );

So the main change I did is tracking error count through second argument of concatMap and i think you want throw an error. You can easily change the condition if needed.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks few errors in the concatMap code but was enough for me to figure it out. The corrections required: if (count === 3 && error.status === 0) {return throwError(error);} return of(count);
if can make the the above amendments I will accept as the answer
Nice ! happy to helped you !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.