3

I am creating an Angular Http Retry Interceptor. When I have retries of 1, my code looks like this:

  return next.handle(req).pipe(
    retryWhen((error) => {
      return error.pipe(
        delay(1000),
        take(1),
        concatMap((err) => {
          return _throw({ ...err, message: `Sorry, there was an error (after 1 retry)` });
        })
      );
    })
  );

This seems to work. I can throw with the original error.

Now, when the retries is zero, this doesn't work. I think that's because we are doing take(0). However, the concat operator will work:

  return next.handle(req).pipe(
    retryWhen((error) => {
      return error.pipe(
        delay(1000),
        take(0),
        concat(_throw({ message: `Sorry, there was an error (after 0 retries)` }))
      );
    })
  );

But the problem is that I can't get the original error. What other operators can I use in my zero retry scenario to get/throw the original error?

techguy2000
  • 4,861
  • 6
  • 32
  • 48

1 Answers1

4

Thanks to paulpdaniels's link. Here is what works for me:

    return next.handle(req).pipe(
      retryWhen((error) => {
        return error.pipe(
          concatMap((e, i) => {
            if (i >= retries) {
              return throwError({ ...e, retries });
            }
            return of(e).pipe(tap(() => log.error(e)), delay(retryInterval as number));
          })
        );
      })
    );

We don't use take anymore and the delay is within concatMap

techguy2000
  • 4,861
  • 6
  • 32
  • 48