捕获 angular dart 中的任何错误(比如 angular 的 ErrorHandler)
catch any error in angular dart (like angular's ErrorHandler)
我需要捕获任何前端 (angulardart) 错误并将其发送回服务器。
我看到在常规 Angular ErrorHandler 中有类似他的东西,但我在 angular 飞镖(或自己飞镖)中找不到任何等价物。
也许我应该破解 Exception 对象的构造函数,但我认为这不是一个好方法(假设它是可能的)
有什么提示吗?
在 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?
我需要捕获任何前端 (angulardart) 错误并将其发送回服务器。
我看到在常规 Angular ErrorHandler 中有类似他的东西,但我在 angular 飞镖(或自己飞镖)中找不到任何等价物。
也许我应该破解 Exception 对象的构造函数,但我认为这不是一个好方法(假设它是可能的)
有什么提示吗?
在 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?