angular - 在 Angular 自定义错误处理程序中使用服务

标签 angular dependency-injection error-handling angular-routing sentry

我在我的项目中实现了以下ErrorHandler:

export class RavenErrorHandler implements ErrorHandler {
  handleError(err: any): void {
    Raven.captureException(err.originalError);
  }
}

我还在我的 AppModule 中正确提供了它。但我还想在我按照上述方法远程记录错误后,使用 AlertService 和路由器将用户路由到一般错误页面。

我如何着手将这些服务注入(inject)自定义 ErrorHandler?

如果我只是将它们注入(inject)构造函数,则应用程序永远不会加载(停留在 Loading...)并且控制台上根本不会显示任何内容!

更新:我在上面的 RavenErrorHandler 中添加了 @Injectable,现在出现以下错误:

[Error] Unhandled Promise rejection: (8) "Provider parse errors: Cannot instantiate cyclic dependency! ApplicationRef (\"[ERROR ->]\"): in NgModule AppModule in ./AppModule@-1:-1" "; Zone:" "
<root>" "; Task:" "Promise.then" "; Value:" Error: Provider parse errors: Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1 — compiler.es5.js:10921 parse — compiler.es5.js:10921 compile — compiler.es5.js:17394
  _compileModule — compiler.es5.js:25652 run — zone.js:141 (anonymous function) — zone.js:805 runTask — zone.js:181 drainMicroTaskQueue — zone.js:574 promiseReactionJob (anonymous function) (vendor.bundle.js:98814) onUnhandledError (polyfills.bundle.js:2613)
  handleUnhandledRejection (polyfills.bundle.js:2637) _loop_1 (polyfills.bundle.js:2628) microtaskDrainDone (polyfills.bundle.js:2632) drainMicroTaskQueue (polyfills.bundle.js:2565) promiseReactionJob [Error] Error: Uncaught (in promise): Error: Provider
  parse errors: Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1 parse@http://localhost:4200/vendor.bundle.js:64024:28 compile@http://localhost:4200/vendor.bundle.js:70497:29 _compileModule@http://localhost:4200/vendor.bundle.js:78755:80
  run@http://localhost:4200/polyfills.bundle.js:2124:49 http://localhost:4200/polyfills.bundle.js:2788:60 runTask@http://localhost:4200/polyfills.bundle.js:2164:57 drainMicroTaskQueue@http://localhost:4200/polyfills.bundle.js:2557:42 promiseReactionJob@[native
  code] — zone.js:757 (anonymous function) (vendor.bundle.js:98814) onUnhandledError (polyfills.bundle.js:2615) handleUnhandledRejection (polyfills.bundle.js:2637) _loop_1 (polyfills.bundle.js:2628) microtaskDrainDone (polyfills.bundle.js:2632) drainMicroTaskQueue
  (polyfills.bundle.js:2565) promiseReactionJob

最佳答案

您不能直接注入(inject) Router,但可以使用注入(inject)器并在 handlerError 方法中获取 Router 实例。

不要忘记将 @Injectable() 装饰器添加到您的自定义处理程序

@Injectable()
export class MyErrorHandler extends ErrorHandler {
  constructor(private service: TestService, private zone: NgZone, private inj: Injector) {
    super();
  }

  handleError(err) {
    let router = this.inj.get(Router);
    debugger;
  }
}

测试一下 here

下面不要看:)这是内在的逻辑

因此,从我们的代码生成的模块工厂将如下所示:

AppModuleInjector.prototype.createInternal = function() {
  var self = this;
  self._CommonModule_0 = new jit_CommonModule27();
  self._TestService_1 = new jit_TestService28();
  self._ErrorHandler_2 = new jit_MyErrorHandler29(self._TestService_1,self.parent.get(jit_NgZone16),self);

关于angular - 在 Angular 自定义错误处理程序中使用服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44086350/

相关文章:

javascript - Angular POST 请求未执行

angular - 使垫子选项和垫子选择具有不同的用户界面

angular - 参数 'error' 隐式具有 'any' 类型

asp.net-mvc - Ninject 获取实例时出现问题

silverlight - 您如何处理Silverlight 4中的验证严重性?

javascript - typescript 'is not a module' 错误

dependency-injection - Rhino 模拟、依赖注入(inject)和关注点分离

java - Spring在配置中指定实现

error-handling - 如何利用简单错误子类提供的格式控制

ruby-on-rails - 收集抛出的RoutingError消息