Created
June 30, 2025 20:13
-
-
Save Silent-Watcher/a5da850cea4632e18a67c81c69b40df2 to your computer and use it in GitHub Desktop.
process handler in node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export function setupProcessHandlers(options?: { | |
| exitOnError?: boolean; | |
| onError?: (type: 'uncaughtException' | 'unhandledRejection', error: any) => void; | |
| }) { | |
| const exit = options?.exitOnError ?? true; | |
| const onError = options?.onError; | |
| process.on('uncaughtException', (err) => { | |
| console.error('[π₯ Uncaught Exception]:', err); | |
| onError?.('uncaughtException', err); | |
| if (exit) process.exit(1); | |
| }); | |
| process.on('unhandledRejection', (reason, promise) => { | |
| console.error('[π₯ Unhandled Rejection]:', reason); | |
| onError?.('unhandledRejection', reason); | |
| if (exit) process.exit(1); | |
| }); | |
| process.on('SIGTERM', () => { | |
| console.log('[π΄ SIGTERM]: Graceful shutdown...'); | |
| process.exit(0); | |
| }); | |
| process.on('SIGINT', () => { | |
| console.log('[π SIGINT]: Graceful shutdown...'); | |
| process.exit(0); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment