捕获角镖中的任何错误(如angular' ErrorHandler)

时间:2017-08-17 15:03:44

标签: dart angular-dart

我需要捕获任何前端(angulardart)错误并将其发送回服务器。

我看到在常规角度ErrorHandler中有类似他的东西,但是我无法在角镖中找到任何等效物(或者自己飞镖)。

也许我应该破解Exception对象的构造函数,但我觉得它不是一个好的方法(假设它可能)

请提供任何提示?

1 个答案:

答案 0 :(得分:2)

在Dart中它非常相似:

@Injectable()
class ErrorHandler implements ExceptionHandler {
  ApplicationRef _appRef;

  ErrorHandler(Injector injector) {
    // prevent DI circular dependency
    new Future<Null>.delayed(Duration.ZERO, () {
      _appRef = injector.get(ApplicationRef) as ApplicationRef;
    });
  }

  @override
  void call(dynamic exception, [dynamic stackTrace, String reason]) {
    final stackTraceParam = stackTrace is StackTrace
        ? stackTrace
        : (stackTrace is String
            ? new StackTrace.fromString(stackTrace)
            : (stackTrace is List
                ? new StackTrace.fromString(stackTrace.join('\n'))
                : null));
    _log.shout(reason ?? exception, exception, stackTraceParam);

    // We can try to get an error shown, but don't assume the app is
    // in a healthy state after this error handler was reached.
    // You can for example still instruct the user to reload the 
    // page with danger to cause hare because of inconsistent
    // application state..
    // To get changes shown, we need to explicitly invoke change detection. 
    _appRef?.tick();
  }
}

提供错误处理程序

return bootstrap(AppComponent, [const Provide(ExceptionHandler, useClass: ErrorHandler)]);

对于可能在Angular之外引起的错误,另请参阅How to catch all uncaught errors in a dart polymer app?