我有一个这样定义的全局错误处理程序(简化/专有信息已清理):
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
到处放,这是我想尽可能避免的事情。