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 623f923

Browse files
committedOct 7, 2024·
feat(@angular/ssr): add attachNodeGlobalErrorHandlers to manage unhandled errors
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.
1 parent 434979a commit 623f923

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed
 

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

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export class AngularNodeAppEngine {
1616
render(request: IncomingMessage, requestContext?: unknown): Promise<Response | null>;
1717
}
1818

19+
// @public
20+
export function attachNodeGlobalErrorHandlers(): void;
21+
1922
// @public
2023
export class CommonEngine {
2124
constructor(options?: CommonEngineOptions | undefined);

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

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export { AngularNodeAppEngine } from './src/app-engine';
1616

1717
export { writeResponseToNodeResponse } from './src/response';
1818
export { createWebRequestFromNodeRequest } from './src/request';
19+
export { attachNodeGlobalErrorHandlers } from './src/errors';
+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)
Please sign in to comment.