angular: APP_INITIALIZER: rejected promise throws "Cannot read property 'ngOriginalError' of undefined"
I’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
The following provider will cause Angular to throw an error.
const INIT_PROVIDER = {
provide: APP_INITIALIZER,
useFactory: initFactory,
multi: true
};
function initFactory() {
return () => init();
}
function init() {
return new Promise((resolve, reject) => {
reject();
});
}
The error occurs here: https://github.com/angular/angular/blob/0cc77b4a6954e563b6eb7999c9d3f08e2ba7dfeb/packages/core/src/errors.ts#L25-L27
ERROR_ORIGINAL_ERROR
('ngOriginalError'
) doesn’t exist on error
at this point.
Related: https://github.com/angular/angular/issues/18523
Expected behavior
The error should not occur. Unsure on what should happen in case the Promise
is rejected but logging an error and continue operating seems like a good solution.
Minimal reproduction of the problem with instructions
https://stackblitz.com/edit/angular-gitter-p6cngp
Open up the console and following error will appear:
Unhandled Promise rejection: Cannot read property ‘ngOriginalError’ of undefined
Environment
Angular version: 5.1.2
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 21 (13 by maintainers)
Commits related to this issue
- Workaround for https://github.com/angular/angular/issues/21252 — committed to nokia/CloudFlow by GuyShaanan 6 years ago
- fix(core): correctly handle `null` or `undefined` in `ErrorHandler#handleError()` Since `ErrorHandler#handleError()` expects an argument of type `any` it should be able to handle values such as `null... — committed to gkalpak/angular by gkalpak 3 years ago
- fix(core): correctly handle `null` or `undefined` in `ErrorHandler#handleError()` Since `ErrorHandler#handleError()` expects an argument of type `any` it should be able to handle values such as `null... — committed to gkalpak/angular by gkalpak 3 years ago
- fix(core): correctly handle `null` or `undefined` in `ErrorHandler#handleError()` (#42881) Since `ErrorHandler#handleError()` expects an argument of type `any` it should be able to handle values such... — committed to angular/angular by gkalpak 3 years ago
I’m also confused as how to handle a rejected promise in this case. Documentation regarding APP_INITIALIZER is not the best.
I definitely wouldn’t want the app to continue starting when APP_INITIALIZER returns a rejected promise.
I believe (at least for the error above) is occurring because you passed an empty
reject()
. If you pass something to itreject('etc etc');
, the above error will go away, but the Application will still not bootstrap successfully.Ideally when using
APP_INITIALIZER
, you want to have some sort of “backup” for when errors occur. Let’s say you called an API to get a JSON configuration that your application needs, in thecatch
try usingresolve({ /* basic app data */}
so that the application can start up (at least with some vague/basic data), you can attempt to fetch the correct data again later after bootstrapping. At least this way, you’ll have the application starting up.@BruneXX put .catch() at the end of your promise. It will hide the error for you.
NOTE: this issue is still happening when reject the promise using APP_INITIALIZER, I’m using Angular 8.x I think you guys (angular team) should take a look at this… Since this issue posted by @SteveVanOpstal is related to angular 5.1.2… a very old version.