Skip to content

Commit 62bf1a8

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 angular/angular#58123
1 parent 7a67752 commit 62bf1a8

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { AngularAppEngine } from '@angular/ssr';
1010
import type { IncomingMessage } from 'node:http';
1111
import type { Http2ServerRequest } from 'node:http2';
12+
import { attachNodeGlobalErrorHandlers } from './errors';
1213
import { createWebRequestFromNodeRequest } from './request';
1314

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

26+
constructor() {
27+
attachNodeGlobalErrorHandlers();
28+
}
29+
2530
/**
2631
* Handles an incoming HTTP request by serving prerendered content, performing server-side rendering,
2732
* or delivering a static file for client-side rendered routes based on the `RenderMode` setting.

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+
* @internal
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)