Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在使用rxjs retryWhen时抛出原始错误,且重试次数为零_Angular_Rxjs_Rxjs6 - Fatal编程技术网

Angular 如何在使用rxjs retryWhen时抛出原始错误,且重试次数为零

Angular 如何在使用rxjs retryWhen时抛出原始错误,且重试次数为零,angular,rxjs,rxjs6,Angular,Rxjs,Rxjs6,我正在创建一个角度Http重试拦截器。当重试次数为1时,我的代码如下所示: return next.handle(req).pipe( retryWhen((error) => { return error.pipe( delay(1000), take(1), concatMap((err) => { return _throw({ ...err, message: `Sorry, ther

我正在创建一个角度Http重试拦截器。当重试次数为1时,我的代码如下所示:

  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)` });
        })
      );
    })
  );
这似乎奏效了。我可以用原来的错误来判断

现在,当重试次数为零时,这不起作用。我想那是因为我们正在做take(0)。但是,concat操作员将:

  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)` }))
      );
    })
  );

但问题是我无法得到原始错误。在我的零重试方案中,我可以使用哪些其他操作符来获取/抛出原始错误?

多亏了paulpdaniels的链接。以下是对我有效的方法:

    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));
          })
        );
      })
    );
我们不再使用take,延迟在concatMap范围内

请参见以下答案: