Angular rxjs throwError重新抛出http错误响应时,自定义全局错误处理程序未命中

Angular rxjs throwError重新抛出http错误响应时,自定义全局错误处理程序未命中,angular,typescript,error-handling,angular-http-interceptors,zone.js,Angular,Typescript,Error Handling,Angular Http Interceptors,Zone.js,目标:为服务器错误和应用程序错误(以Typescript代码生成)提供全局错误处理程序 方法:从同一工作区内的lib项目提供自定义ErrorHandler。这是我的库结构: 我有以下http拦截器(http error.interceptor.ts) 这是错误处理.module.ts import { NgModule, ErrorHandler } from '@angular/core'; import { HTTP_INTERCEPTORS } from '@angular/common

目标:为服务器错误和应用程序错误(以Typescript代码生成)提供全局错误处理程序

方法:从同一工作区内的lib项目提供自定义ErrorHandler。这是我的库结构:

我有以下http拦截器(http error.interceptor.ts)

这是错误处理.module.ts

import { NgModule, ErrorHandler } from '@angular/core';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpErrorInterceptor } from './http-error.interceptor';
import { ErrorsHandler } from './errors-handler';

    @NgModule({
      declarations: [],
      imports: [
      ],
      exports: [],
      providers: [
        {provide: ErrorHandler, useClass: ErrorsHandler},
        {provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true}
      ]
    })
    export class ErrorHandlingModule { }
在我的public_api.ts文件中,我只导出模块

/*
 * Public API Surface of error-handling
 */

export * from './lib/error-handling.module';
在同一个工作区中,我有一个应用程序(Angular CLI提供的默认应用程序,它不在projects文件夹中)。在我的app.module.ts中,我导入了ErrorHandlingModule:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';

import { ErrorHandlingModule } from '@common/error-handling';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { CoreModule } from './core/core.module';



@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    CoreModule,

    ErrorHandlingModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
我已经构建了@common/错误处理库。 我还有一个带有json服务器的假api,我在其中定义了一个“categories”端点。我在onInit方法中的app.component.ts上调用该服务,通过调用端点“category”(不带“s”)生成404HTTP错误响应。当我为我的应用程序提供服务时,控制台中有以下内容:

404错误就在那里,我还可以从http-error.interceptor.ts中看到“error”日志,但是我无法从我的自定义ErrorHandler中看到“hi!”日志

在调用伪api端点后,当我从app.component.ts抛出错误时,全局错误处理程序正在工作。不知怎的,这条线

return throwError(error);
在http-error.interceptor.ts中,未到达我的全局错误处理程序

它是否与zone.js相关?错误会被抛出到那里,而不会冒泡到应用程序的其余部分。我对zone.js不太熟悉

还有其他想法吗

提前谢谢

最好的, 马克斯

然后在AppModule导入中使用
ErrorHandlingModule.forRoot()


对于额外信息-

如果您的被观察者接受错误回调,则抛出者操作符将返回给观察者,并且不会被全局错误拦截器拾取

只需在subscribe中注释catchError块


谢谢你的回答。这个问题真的很愚蠢。我有一个用作基本服务的类(从这个基本服务继承的所有服务),在那里我有一个catchError,在出错时返回默认值。因为这是该链的最后一个捕获,所以我没有点击全局错误处理程序。很抱歉浪费您的时间,谢谢您再次回答!当我从拦截器抛出错误时,ErrorHandler无法捕捉到它为什么?@MuhammedOzdogan我不理解你的问题。您能重新格式化它吗?在HttpErrorInterceptor类中有这样一个语句:“return-throwError(error);”。“Errrorshandler”应该知道吗?
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';

import { ErrorHandlingModule } from '@common/error-handling';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { CoreModule } from './core/core.module';



@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    HttpClientModule,
    CoreModule,

    ErrorHandlingModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
return throwError(error);
export class ErrorHandlingModule { 
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: ErrorHandlingModule,
      providers: [
        {provide: ErrorHandler, useClass: ErrorsHandler},
        {provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true} ]
    };
}
getWorkData(id: string): void {
this.userDataService.getWorkData(id).subscribe(
  (response) => {
    this.Id = response.Id;
    this.localStorage.setItem('WorkData', response);
  }
  // (err) => {
  //   //this.monitoringService.logException(err);
  // },
);
}