映射Angular HttpClient错误及其类型

时间:2020-10-04 15:00:13

标签: angular typescript rxjs rxjs6

我想用其类型映射HttpClient的错误,并且我有以下代码:

export class ErrorMessageInterceptor implements HttpInterceptor {
  constructor(private notifyService: NotifyService) {}

  public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
      catchError(error => {
        console.error(error);
        return throwError({
          ok: error.ok,
          status: error.status,
          statusText: error.statusText,
          code: error.error.error.code,
          message: error.error.error.message,
          headers: error.headers,
          url: error.url,
        } as OdataError);
      }),
      tap(() => {}, async error => {
        await this.notifyService.error(error.message);
      }),
    );
  }
}

但是不幸的是,error运算符中tap的默认类型仍然是any,并且我不想将错误类型映射到将要使用的任何位置。所以我在这里有两个问题:

  1. 为什么在上面的代码中看不到映射的类型错误?
  2. 是否有办法全局设置自定义错误类型,或者至少为注入的HttpClient实例设置自定义错误类型?

0 个答案:

没有答案