-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
renderModuleFactory does not reject when error is thrown in application #33642
Comments
cc @vikerman |
I have looked into your reproduction. This exception is caught during the very first change detection. angular/packages/core/src/application_ref.ts Lines 682 to 694 in 346e1c1
You can just override the default @Injectable()
export class ServerErrorHandler implements ErrorHandler {
handleError(error: Error): void {
throw error;
}
}
@NgModule({
imports: [AppModule, ServerModule],
bootstrap: [AppComponent],
providers: [
{
provide: ErrorHandler,
useClass: ServerErrorHandler
}
]
})
export class AppServerModule {} And do something like: try {
const html = await renderModuleFactory(AppServerModuleNgFactory, {
document: index
});
} catch (e) {
console.log(e);
} |
Thank you so much! Will give that a try. We probably don't want the default to swallow the error though... Do we? |
Currently, when there is an error inside a lazy loaded component contructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ````ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a set closer to address angular#33642
Currently, when there is an error inside a lazy loaded component contructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ````ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
Currently, when there is an error inside a lazy loaded component contructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ````ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
Currently, when there is an error inside a lazy loaded component constructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ```ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
Currently, when there is an error inside a lazy loaded component constructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ```ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
Currently, when there is an error inside a lazy loaded component constructor an unhandled promise rejection is thrown which causes the error handler not to be invoked on the server due to an issue with Zone.js see angular#49930. While this is a workaround for that in general we should not cause unhandled promise rejections and always await and catch promises especially going for going zoneless, this is because Node.js does not wait for pending unhandled promises and these will cause the process to exit. See: nodejs/node#22088 Eventually, the framework will need to be updated to better handle promises in certain listeners for zoneless and avoid unhandled promises Example on potential problematic code. ```ts { provide: APP_BOOTSTRAP_LISTENER, useFactory: () => { Promise.resolve().then(() => {}) }, } ``` This change is also a step closer to address angular#33642
IMHO those are criteria of an ideal solution: (What do you think @alan-agius4?)
|
I believe there is an architectural/business decision to be made: should errors be propagated immediately when they occur VS. lately only after the app is stable and the rendering finished? Errors propagated immediately:Pros:
Cons:
Errors propagated lately (only after app is stable):Pros:
Cons:
I have hard time to choose. My heart says the "late" errors propagation is better, because it's 100% safe and predictable. But my brain tells me it's not as performant as could be. Just curious, which option you're more fond of, @alan-agius4 ? |
By the way, when we agree on the final architectural design, I'm happy to contribute with a PR, if it works for you. The topic of Angular SSR really interests me 😉 |
Hi @alan-agius4, sorry for bugging you. I'm aware you have lot on your plate. PS. the duplicate of this issue (#50829) has 23 upvotes 👍 |
@Platonn, this is definitely something that I want to get too at some point. If you'd like to do a PR feel free to try, although I suspect it is not a trivial change. |
Hi @alan-agius4 , thanks for your reply. Could you help me understand please, which aspects do you perceive as not trivial?
If there is anything more I should give a special attention to, please let me know. I'm happy to adapt. Thanks in advance! |
…lizing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). This commit partially resolves issue angular#33642. However, it's important to note that asynchronous lifecycle hooks or hooks containing promises are not currently addressed. This limitation stems from hooks within the framework being floating promises, causing such errors to manifest as unhandled rejections.
…ks when utilizing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
…izing platform-server In previous versions, if an error occurred within the lifecycle hooks, the promises for `renderModule` and `renderApplication` were not rejected. This could result in serving a "broken" page during Server-Side Rendering (SSR), or the build erroneously being marked as successful during Static Site Generation (SSG). Fixes angular#33642
🐞 bug report
Affected Package
@angular/platform-server
Is this a regression?
No, I tested a v4 and v6 and the behavior is consistent
Description
renderModuleFactory
does not reject for errors thrown during rendering.Examples:
@Component
ngOnInit
resolve
🔬 Minimal Reproduction
https://github.com/FrozenPandaz/angular-bugs/tree/render-module-factory
npm i && npm run prerender
🔥 Exception or Error
🌍 Your Environment
Angular Version:
The text was updated successfully, but these errors were encountered: