admin管理员组文章数量:1277336
An uncaught exception (injector or just random error) in any ponent during bootstrap may lead to bootstrap failure.
Due to browser inpatibility the app may not start bootstrap process at all.
I'm looking for a way to notify a user (with alert, modal, etc) on critical error that happened during Angular 2 app initialization, so a user could be sure that the app not just loads forever but failed for some reason.
What's the good recipe to handle this case in Angular 2?
An uncaught exception (injector or just random error) in any ponent during bootstrap may lead to bootstrap failure.
Due to browser inpatibility the app may not start bootstrap process at all.
I'm looking for a way to notify a user (with alert, modal, etc) on critical error that happened during Angular 2 app initialization, so a user could be sure that the app not just loads forever but failed for some reason.
What's the good recipe to handle this case in Angular 2?
Share Improve this question edited Nov 3, 2016 at 0:47 Estus Flask asked Nov 2, 2016 at 21:51 Estus FlaskEstus Flask 223k78 gold badges471 silver badges608 bronze badges 3-
1
How about
platformBrowserDynamic().bootstrapModule(<AppName>).catch(err => console.error(err));
– Bhavik Commented Nov 5, 2016 at 6:38 -
1
@Bhavik That would be a good place to catch it. But it can't catch
Can't resolve all parameters for ...
injector errors (the promise is pending), and who knows what else. – Estus Flask Commented Nov 5, 2016 at 12:36 -
1
@Bhavik There was a bug that prevented bootstrapModule promise from catching all errors. It has been fixed in
master
, and for previous versionsPromise.resolve().then(() => platformBrowserDynamic().bootstrapModule(...)).catch(...))
solves the problem. Feel free to post this as an answer. – Estus Flask Commented Nov 11, 2016 at 17:57
2 Answers
Reset to default 4 +150PlatformRef.bootstrapModule
return's a promise where we can .catch
the error, while bootstrap
(initializing) the app.
platformBrowserDynamic().bootstrapModule(<AppName>)
.catch(err => console.error(err));
There was a bug, which was just resolved(changelog). Thanks to @estus, for pointing it out.
It was possible to fix this with altered deferred pattern. However, the solution is based on the assumption that bootstrapping is synchronous and happens within one tick, which may be wrong.
Still not sure if there are better approaches.
class InitDeferred {
promise: Promise<any>;
private fulfilled: boolean;
constructor() {
this.promise = new Promise((resolve, reject) => {
setTimeout(() => this.fulfilled === true ? resolve() : reject('init error'));
});
}
resolve() {
this.fulfilled = true;
}
reject() {
this.fulfilled = false;
}
}
const initDeferred = new InitDeferred;
export default initDeferred;
@Injectable()
export class InitErrorHandler extends ErrorHandler {
handleError(error: any): void {
initDeferred.reject();
super.handleError(error);
}
}
@Component(...)
export class App extends OnInit {
ngOnInit() {
initDeferred.resolve();
}
}
@NgModule({
...
providers: [{ provide: ErrorHandler, useClass: InitErrorHandler }]
})
export class AppModule {}
initDeferred.promise.catch(alert);
platformBrowserDynamic().bootstrapModule(AppModule);
本文标签: javascriptNotify on Angular 2 app initialization errorStack Overflow
版权声明:本文标题:javascript - Notify on Angular 2 app initialization error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741288934a2370434.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论