Kon*_*Kon 6 error-handling typescript angular angular-errorhandler angular7
我有这样定义的全局错误处理程序(清除了简化/专有信息):
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;
}
}
Run Code Online (Sandbox Code Playgroud)
并且在我的模块(并且只有我的模块)中,它被注册为提供者,如下所示:
export function errorHandlerFactory(loggingService: LoggingService, coreService: CoreService) {
return new ErrorsHandler(loggingService, coreService);
}
providers: [
{ provide: ErrorHandler, useFactory: errorHandlerFactory, deps: [LoggingService, CoreService] }
]
Run Code Online (Sandbox Code Playgroud)
我的模块被其他人使用,我们一起组成了一个大型应用程序。我的问题是,即使我尝试过滤仅与我的模块/软件包相关的那些脚本,也会捕获到所有脚本错误,因为过滤是在中完成的handleError()。即使我重新抛出了与我无关的错误(在else上面),其他模块/程序包的开发人员仍在抱怨我在全球范围内捕获所有内容,并且重新抛出的错误已经失去了某些上下文/信息。
所以问题是,是否有可能以某种方式限制我的错误处理程序的范围,以仅捕获和处理源自我的模块/包的脚本错误(而完全忽略应用程序中的所有其他脚本错误)?
经过大量的搜寻之后,我唯一想到的替代方法是将try/catch其放置在任何地方,这是我想尽可能避免的方法。
您可以尝试创建一个服务 -ErrorService共享context,然后throw共享global error handler. 然后您可以catch从所需的错误Component。
PFB的步骤:
创建错误服务如下:
@Injectable({
providedIn: 'root'
})
export class ErrorService {
constructor() {}
private error = new BehaviorSubject(new Error());
error_message = this.error.asObservable();
changeMessage(e: Error) {
this.error.next(e);
}
}
Run Code Online (Sandbox Code Playgroud)throwhandleError中方法的错误ErrorHandler。PFB 片段:
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 {
//`errorService` is the `instance` for `ErrorService Component`
//imported in the defined `ErrorHandler`
this.errorService.changeMessage(error);
// Rethrow all other errors.
throw error;
}
}
Run Code Online (Sandbox Code Playgroud)用于try-catch捕获您的Component. 使用error_messagefrom 的ErrorService效果相同。
| 归档时间: |
|
| 查看次数: |
284 次 |
| 最近记录: |