目标:为服务器错误和应用程序错误(用 Typescript 代码生成)提供一个全局错误处理程序。
如何:从同一工作区中的 lib 项目提供自定义 ErrorHandler。这是我的库结构:
我有以下 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