Skip to content

Commit 905df8f

Browse files
committed
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 905df8f

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

Diff for: packages/angular/ssr/node/public_api.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ 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';
2019
export { isMainModule } from './src/module';
20+
export { attachNodeGlobalErrorHandlers } from './src/errors';

Diff for: 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
*

Diff for: 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

Diff for: packages/angular/ssr/node/src/errors.ts

+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 & { ngAtachNodeGlobalErrorHandlersCalled?: boolean } = globalThis;
20+
if (gThis.ngAtachNodeGlobalErrorHandlersCalled) {
21+
return;
22+
}
23+
24+
gThis.ngAtachNodeGlobalErrorHandlersCalled = 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+
}
File renamed without changes.

0 commit comments

Comments
 (0)