Skip to content

Commit 319b8e0

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 92e193c commit 319b8e0

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

Diff for: goldens/public-api/angular/ssr/node/index.api.md

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

1515
// @public
1616
export class AngularNodeAppEngine {
17+
constructor();
1718
handle(request: IncomingMessage | Http2ServerRequest, requestContext?: unknown): Promise<Response | null>;
1819
}
1920

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

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

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
* @remarks
15+
* This function is a no-op if zone.js is available.
16+
* For Zone-based apps, similar functionality is provided by Zone.js itself. See the Zone.js implementation here:
17+
* https://github.com/angular/angular/blob/4a8d0b79001ec09bcd6f2d6b15117aa6aac1932c/packages/zone.js/lib/node/node.ts#L94%7C
18+
*
19+
* @internal
20+
*/
21+
export function attachNodeGlobalErrorHandlers(): void {
22+
if (typeof Zone !== 'undefined') {
23+
return;
24+
}
25+
26+
// Ensure that the listeners are registered only once.
27+
// Otherwise, multiple instances may be registered during edit/refresh.
28+
const gThis: typeof globalThis & { ngAttachNodeGlobalErrorHandlersCalled?: boolean } = globalThis;
29+
if (gThis.ngAttachNodeGlobalErrorHandlersCalled) {
30+
return;
31+
}
32+
33+
gThis.ngAttachNodeGlobalErrorHandlersCalled = true;
34+
35+
process
36+
// eslint-disable-next-line no-console
37+
.on('unhandledRejection', (error) => console.error('unhandledRejection', error))
38+
// eslint-disable-next-line no-console
39+
.on('uncaughtException', (error) => console.error('uncaughtException', error));
40+
}

Diff for: packages/angular/ssr/node/src/globals.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
declare const Zone: unknown | undefined;
File renamed without changes.

0 commit comments

Comments
 (0)