代码之家  ›  专栏  ›  技术社区  ›  Kon

如何限制错误处理程序的范围?

  •  0
  • Kon  · 技术社区  · 5 年前

    我定义了一个全局错误处理程序(简化/专有信息清除):

    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 在任何地方,如果可能的话,这是我想避免的事情。

    0 回复  |  直到 5 年前
        1
  •  3
  •   Jahnavi Paliwal    5 年前

    您可以尝试创建服务- ErrorService 分享 context 然后 throw 错误来自 global error handler . 那你就可以了 catch 所需的错误 Component .

    PFB步骤:

    1. 创建错误服务如下:

      @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);
          }
      }
      
    2. 错误来自 handleError 方法 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;
           }
        }
      
    3. 使用 try-catch 在您的 组件 . 使用 error_message 错误服务 同样的。

        2
  •  1
  •   nullchimp    5 年前

    我不知道什么 CommonBase 正在做,因此我不确定这是否是一个可行的答案,但你可以做的一件事是改变你的 ErrorsHandler 有点。如果你改变你的结构,从核心衍生出来 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);
      }
    }
    

    我认为这会给你一个更可靠的方法,并且会正确地传播错误。重新抛出错误不知何故不能正常工作,但我不能100%确定确切的原因。

    希望有帮助!

    推荐文章