[英]How to limit scope of ErrorHandler?
I have a global error handler defined as such (simplified/proprietary info scrubbed): 我有一个全局错误处理程序定义(简化/专有信息擦除):
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;
}
}
And in my module (and only my module), it's registered as a provider as such: 在我的模块(只有我的模块)中,它被注册为提供者:
export function errorHandlerFactory(loggingService: LoggingService, coreService: CoreService) {
return new ErrorsHandler(loggingService, coreService);
}
providers: [
{ provide: ErrorHandler, useFactory: errorHandlerFactory, deps: [LoggingService, CoreService] }
]
My module is consumed by others, and together we make up one large application. 我的模块被其他人使用,我们一起组成一个大型应用程序。 My problem is that ALL script errors are caught, even though I try to filter for those that are only relevant to my module/package, because the filtering is done within
handleError()
. 我的问题是所有脚本错误都被捕获,即使我尝试过滤那些仅与我的模块/包相关的错误,因为过滤是在
handleError()
内完成的。 And even though I rethrow errors that are not relevant to me (in the else
above), developers of other modules/packages are complaining that I'm globally catching everything and the rethrown errors they get already lose certain context/information. 即使我重新抛出与我无关的错误(在上面的
else
),其他模块/包的开发人员抱怨我全局捕获所有内容并且他们已经丢失了某些上下文/信息的重新抛出错误。
So the question is, is it possible to somehow limit the scope of my error handler to catch and handle ONLY script errors originating from my module/package (while completely ignore all other script errors within the application)? 所以问题是,是否有可能以某种方式限制我的错误处理程序的范围,以捕获和处理源自我的模块/包的脚本错误(同时完全忽略应用程序中的所有其他脚本错误)?
After much googling, the only alternative I can think of is putting try/catch
everywhere, which is something I'd like to avoid if at all possible. 经过大量的谷歌搜索,我能想到的唯一选择是将
try/catch
放到任何地方,这是我想尽可能避免的事情。
You can try creating a service - ErrorService
to share the context
and then throw
the error from the global error handler
. 您可以尝试创建服务 -
ErrorService
来共享context
,然后从global error handler
throw
global error handler
。 You can then catch
the error from the required Component
. 然后,您可以从所需的
Component
catch
错误。
PFB the steps: PFB的步骤:
Create the error service as follows: 创建错误服务,如下所示:
@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); } }
throw
the error from the handleError
method in ErrorHandler
. 从
ErrorHandler
的handleError
方法throw
错误。 PFB the snippet: 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; } }
Use try-catch
to catch the error in your Component
. 使用
try-catch
来捕获Component
的错误。 Use error_message
from ErrorService
for the same. 使用
ErrorService
中的error_message
ErrorService
。
I don't know what CommonBase
is doing, and therefore I am not sure if this is a feasible answer, but one thing that you could do is change your ErrorsHandler
a little bit. 我不知道
CommonBase
在做什么,因此我不确定这是否是一个可行的答案,但你能做的一件事就是改变你的ErrorsHandler
。 If you change your structure and derive from the core ErrorHandler
instead of implementing its interface you could end up with something like this: 如果您更改结构并从核心
ErrorHandler
派生而不是实现其接口,您最终可能会得到如下内容:
import { Injectable, ErrorHandler } from '@angular/core';
@Injectable()
export class ErrorsHandler extends ErrorHandler {
constructor() {
super();
}
handleError(error: any): void {
if(error && error.stack && error.stack.indexOf("MyModule") >= 0) {
console.log(`Uff, that was close. Got ${error}`);
return;
}
super.handleError(error);
}
}
I figured that this gives you a much more reliability and will propagate errors correctly. 我认为这会为您提供更高的可靠性并正确传播错误。 Re-throwing the error doesn't work properly somehow, but I am not 100% sure about the exact reason.
重新抛出错误不能以某种方式正常工作,但我不是100%肯定的确切原因。
I hope that helps! 我希望有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.