小编Max*_*llo的帖子

当 rxjs 重新抛出 http 错误响应时,自定义全局错误处理程序不会被命中

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

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

@common/错误处理库

我有以下 http-interceptor (http-error.interceptor.ts)

@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {

  constructor(@Inject(LOGGER_SERVICE) private logger: ILoggerService) {}

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req)
    .pipe(
        catchError( (error: HttpErrorResponse) => {
          console.log('error');

          return throwError(error);
        })
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

以下自定义全局错误处理程序(errors-handler.ts):

import { ErrorHandler, Injectable } from '@angular/core';

@Injectable()
export class ErrorsHandler implements ErrorHandler {

    handleError(error: any): void {
        console.log('hi!');
    }

}
Run Code Online (Sandbox Code Playgroud)

这是错误处理.module.ts

import { NgModule, ErrorHandler } from '@angular/core';
import { HTTP_INTERCEPTORS } from …
Run Code Online (Sandbox Code Playgroud)

error-handling typescript angular-http-interceptors zone.js angular

6
推荐指数
1
解决办法
6121
查看次数