Skip to content

Instantly share code, notes, and snippets.

@Silent-Watcher
Created June 30, 2025 20:13
Show Gist options
  • Save Silent-Watcher/a5da850cea4632e18a67c81c69b40df2 to your computer and use it in GitHub Desktop.
Save Silent-Watcher/a5da850cea4632e18a67c81c69b40df2 to your computer and use it in GitHub Desktop.
process handler in node.js
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