Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 81f7fc6

Browse files
committedOct 7, 2024·
fix(@angular/ssr): manage unhandled errors in zoneless applications
Implement the `attachNodeGlobalErrorHandlers` function to handle 'unhandledRejection' and 'uncaughtException' events in Node.js. This function logs errors to the console, preventing unhandled errors from crashing the server. It is particularly useful for zoneless apps, ensuring error handling without relying on zones. Closes #28581
1 parent 54cb005 commit 81f7fc6

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed
 

‎goldens/public-api/angular/ssr/node/index.api.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Type } from '@angular/core';
1212

1313
// @public
1414
export class AngularNodeAppEngine {
15+
constructor();
1516
getPrerenderHeaders(request: IncomingMessage): ReadonlyMap<string, string>;
1617
render(request: IncomingMessage, requestContext?: unknown): Promise<Response | null>;
1718
}

‎packages/angular/ssr/node/public_api.ts

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export {
1313
} from './src/common-engine/common-engine';
1414

1515
export { AngularNodeAppEngine } from './src/app-engine';
16-
1716
export { createNodeRequestHandler } from './src/handler';
1817
export { writeResponseToNodeResponse } from './src/response';
1918
export { createWebRequestFromNodeRequest } from './src/request';

‎packages/angular/ssr/node/src/app-engine.ts

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import { AngularAppEngine } from '@angular/ssr';
1010
import type { IncomingMessage } from 'node:http';
11+
import { attachNodeGlobalErrorHandlers } from './errors';
1112
import { createWebRequestFromNodeRequest } from './request';
1213

1314
/**
@@ -23,6 +24,10 @@ import { createWebRequestFromNodeRequest } from './request';
2324
export class AngularNodeAppEngine {
2425
private readonly angularAppEngine = new AngularAppEngine();
2526

27+
constructor() {
28+
attachNodeGlobalErrorHandlers();
29+
}
30+
2631
/**
2732
* Renders an HTTP response based on the incoming request using the Angular server application.
2833
*

‎packages/angular/ssr/node/src/common-engine/common-engine.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/plat
1111
import * as fs from 'node:fs';
1212
import { dirname, join, normalize, resolve } from 'node:path';
1313
import { URL } from 'node:url';
14+
import { attachNodeGlobalErrorHandlers } from '../errors';
1415
import { CommonEngineInlineCriticalCssProcessor } from './inline-css-processor';
1516
import {
1617
noopRunMethodAndMeasurePerf,
@@ -63,7 +64,9 @@ export class CommonEngine {
6364
private readonly inlineCriticalCssProcessor = new CommonEngineInlineCriticalCssProcessor();
6465
private readonly pageIsSSG = new Map<string, boolean>();
6566

66-
constructor(private options?: CommonEngineOptions) {}
67+
constructor(private options?: CommonEngineOptions) {
68+
attachNodeGlobalErrorHandlers();
69+
}
6770

6871
/**
6972
* Render an HTML document for a specific URL with specified
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
/**
10+
* Attaches listeners to the Node.js process to capture and handle unhandled rejections and uncaught exceptions.
11+
* Captured errors are logged to the console. This function logs errors to the console, preventing unhandled errors
12+
* from crashing the server. It is particularly useful for Zoneless apps, ensuring error handling without relying on Zone.js.
13+
*
14+
* @developerPreview
15+
*/
16+
export function attachNodeGlobalErrorHandlers(): void {
17+
// Ensure that the listeners are registered only once.
18+
// Otherwise, multiple instances may be registered during edit/refresh.
19+
const gThis: typeof globalThis & { ngAttachNodeGlobalErrorHandlersCalled?: boolean } = globalThis;
20+
if (gThis.ngAttachNodeGlobalErrorHandlersCalled) {
21+
return;
22+
}
23+
24+
gThis.ngAttachNodeGlobalErrorHandlersCalled = true;
25+
26+
process
27+
// eslint-disable-next-line no-console
28+
.on('unhandledRejection', (error) => console.error('unhandledRejection', error))
29+
// eslint-disable-next-line no-console
30+
.on('uncaughtException', (error) => console.error('uncaughtException', error));
31+
}

0 commit comments

Comments
 (0)
Please sign in to comment.