0

我遇到了错误处理程序的问题。我正在尝试在我的服务中创建一个通用的 retryPipeline:当调用失败时,它会在抛出和错误之前重试 3 次。到现在为止还挺好。如果我将代码放在方法中,它会起作用,如下所示:

  getMun(id_del: number, id_muno: number): Observable<Mun> {

    let urlAPIMun = urlAPI;
    urlAPIMun += '/' + id_del + '/mun' + '/' + id_mun + '?flag_geometry=true';
    return this._http.get<Mun>(urlAPIMunicipios).pipe(
     //   tap(() => console.log('HTTP request executed')),
      retryWhen(errors => errors.pipe(
        // Concat map to keep the errors in order and make sure they
        // aren't executed in parallel
        concatMap((e: HttpErrorResponse, i) =>
          // Executes a conditional Observable depending on the result
          // of the first argument
          iif(
            () => i >= 3,
            // If the condition is true we throw the error (the last error)
            throwError(e.error.errores),
            // Otherwise we pipe this back into our stream and delay the retry
            of(e).pipe(delay(5000))
          )
        ))));
  }

我试图提取管道内的代码来声明一个常量,然后在我的服务调用中调用常量:

const RETRYPIPELINE =
  retryWhen((errors) =>
    errors.pipe(
      concatMap((e: HttpErrorResponse, i) =>
          () => i >= 3,
          throwError(e.error.errores),
          of(e).pipe(delay(5000))
        )
      )
    )
  );

return this._http.get<Mun>(urlAPIMunicipios).pipe(RETRYPIPELINE);

但我收到此错误:

错误 TS2322:类型 'Observable<{}>' 不可分配给类型 'Observable'。类型“{}”缺少类型“Mun”的以下属性:id_mun、id_del、den

有什么方法可以创建一个可以分配给任何方法的通用 const,尽管该方法返回一个类型化的值?提前致谢

4

1 回答 1

0

最后,感谢这个答案,我修复了它:

添加演员表retryWhen,彻底解决我的问题:

export const RETRYPIPELINE =
  retryWhen<any>((errors) =>
    errors.pipe(
      // Use concat map to keep the errors in order and make sure they
      // aren't executed in parallel
      concatMap((e: HttpErrorResponse, i) =>
        // Executes a conditional Observable depending on the result
        // of the first argument
        iif(
          () => i >= 3,
          // If the condition is true we throw the error (the last error)
          throwError(e.error.errores),
          // Otherwise we pipe this back into our stream and delay the retry
          of(e).pipe(delay(5000))
        )
      )
    )
  )

;

于 2019-04-10T11:01:38.707 回答