gpt4 book ai didi

dart - 捕获角度 Dart 中的任何错误(例如angular的ErrorHandler)

转载 作者:行者123 更新时间:2023-12-03 02:57:07 25 4
gpt4 key购买 nike

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

我在常规的Angular ErrorHandler中看到了类似他的东西,但是我找不到等效的angart Dart (或自我 Dart )。

也许我应该破解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?

关于dart - 捕获角度 Dart 中的任何错误(例如angular的ErrorHandler),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45738697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com