Skip to content

Commit 95afc9c

Browse files
committed
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 95afc9c

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

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

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

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

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
process
18+
// eslint-disable-next-line no-console
19+
.on('unhandledRejection', (error) => console.error('unhandledRejection', error))
20+
// eslint-disable-next-line no-console
21+
.on('uncaughtException', (error) => console.error('uncaughtException', error));
22+
}

0 commit comments

Comments
 (0)