我定义了一个全局错误处理程序(简化/专有信息清除):
export class ErrorsHandler extends CommonBase implements ErrorHandler {
constructor(protected loggingService: LoggingService,
private coreService: CoreService {
super(loggingService);
}
handleError(error: Error) {
if (error && error.stack && (error.stack.indexOf(Constants.PACKAGE_NAME) >= 0)) {
this.logSystemError(error, true);
this.coreService.showSystemError(error, this.owner);
}
else {
// Rethrow all other errors.
throw error;
}
}
在我的模块(仅我的模块)中,它注册为这样的提供者:
export function errorHandlerFactory(loggingService: LoggingService, coreService: CoreService) {
return new ErrorsHandler(loggingService, coreService);
}
providers: [
{ provide: ErrorHandler, useFactory: errorHandlerFactory, deps: [LoggingService, CoreService] }
]
我的模块被其他人使用,我们一起组成一个大型应用程序。
我的问题是,所有脚本错误都会被捕获,即使我试图过滤那些只与我的模块/包相关的错误,因为过滤是在
handleError()
. 即使我重述了与我无关的错误(在
else
上面),其他模块/包的开发人员抱怨说,我在全球范围内捕捉到了所有东西,并且它们所得到的重新引发的错误已经失去了一定的上下文/信息。
所以问题是,是否可以以某种方式限制错误处理程序的范围,只捕获和处理来自模块/包的脚本错误(同时完全忽略应用程序中的所有其他脚本错误)?
在谷歌搜索了很多之后,我唯一能想到的选择就是
try/catch
在任何地方,如果可能的话,这是我想避免的事情。