为什么错误处理程序在我的angular 7应用程序中没有捕获任何错误? /**添加产品*/ addProduct(产品:产品):可观察{ var url=`${this.apirl}/Products`; 返回此.http.post(url,product.pipe)( 点击((newProduct:Product)=>this.log(`added Product w/id=${newProduct.id}'), catchError(this.handleError('addProduct')) ); } /** *处理失败的Http操作。 *让应用程序继续。 *@param operation-失败的操作的名称 *@param result-作为可观察结果返回的可选值 */ 私有句柄错误(操作='operation',结果?:T){ 返回(错误:任意):可观察=>{ //TODO:将错误发送到远程日志记录基础结构 console.error(error);//改为登录到控制台 //TODO:更好地转换错误以供用户使用 此.log(`operation}失败:${error.message}`); //通过返回空结果让应用程序继续运行。 返回(结果); }; } /**使用MessageService记录ActionService消息*/ 私有日志(消息:字符串){ 控制台日志(消息); }

为什么错误处理程序在我的angular 7应用程序中没有捕获任何错误? /**添加产品*/ addProduct(产品:产品):可观察{ var url=`${this.apirl}/Products`; 返回此.http.post(url,product.pipe)( 点击((newProduct:Product)=>this.log(`added Product w/id=${newProduct.id}'), catchError(this.handleError('addProduct')) ); } /** *处理失败的Http操作。 *让应用程序继续。 *@param operation-失败的操作的名称 *@param result-作为可观察结果返回的可选值 */ 私有句柄错误(操作='operation',结果?:T){ 返回(错误:任意):可观察=>{ //TODO:将错误发送到远程日志记录基础结构 console.error(error);//改为登录到控制台 //TODO:更好地转换错误以供用户使用 此.log(`operation}失败:${error.message}`); //通过返回空结果让应用程序继续运行。 返回(结果); }; } /**使用MessageService记录ActionService消息*/ 私有日志(消息:字符串){ 控制台日志(消息); },angular,Angular,我正在使用HttpInterceptor。这与防止在服务文件中处理错误有关吗?我应该处理每个服务文件的错误还是通过公共拦截器处理错误?如果您在HttpInterceptor中捕获错误,您可能需要重新显示该错误。如果您在拦截器catchError中捕获错误((错误:HttpErrorResponse)=>{return throwError(错误);}),您是否尝试不使用HttpInterceptor?要查看它是否正常工作,其目的不是删除拦截器,因为它负责消息。即使它是这样工作的,它对我也没有多大

我正在使用HttpInterceptor。这与防止在服务文件中处理错误有关吗?我应该处理每个服务文件的错误还是通过公共拦截器处理错误?

如果您在HttpInterceptor中捕获错误,您可能需要重新显示该错误。如果您在拦截器catchError中捕获错误((错误:HttpErrorResponse)=>{return throwError(错误);}),您是否尝试不使用HttpInterceptor?要查看它是否正常工作,其目的不是删除拦截器,因为它负责消息。即使它是这样工作的,它对我也没有多大用处,因为我想处理每个服务文件的错误日志记录
/** Add a product */
 addProduct(product: Product): Observable<Product> {
        var url = `${this.apiUrl}/Products`;
        return this.http.post<Product>(url, product).pipe(
          tap((newProduct: Product) => this.log(`added Product w/ id=${newProduct.id}`)),
          catchError(this.handleError<Product>('addProduct'))
        );
      }

 /**
   * Handle Http operation that failed.
   * Let the app continue.
   * @param operation - name of the operation that failed
   * @param result - optional value to return as the observable result
   */
  private handleError<T>(operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // TODO: better job of transforming error for user consumption
      this.log(`${operation} failed: ${error.message}`);

      // Let the app keep running by returning an empty result.
      return of(result);
    };
  }

  /** Log a ActionService message with the MessageService */
  private log(message: string) {
    console.log(message);
  }