@schemifyjs/cli
Version:
Official CLI for SchemifyJS.
41 lines (40 loc) • 1.29 kB
JavaScript
import { ErrorHandler } from './error-handler.js';
/**
* Global error handler for unhandled promise rejections and exceptions
* Similar to NestJS global exception filter
*/
export function setupGlobalErrorHandling() {
// Handle unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Promise Rejection:');
ErrorHandler.handle(reason);
process.exit(1);
});
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:');
ErrorHandler.handle(error);
process.exit(1);
});
// Handle SIGINT (Ctrl+C)
process.on('SIGINT', () => {
console.log('\n\nOperation cancelled by user.');
process.exit(0);
});
// Handle SIGTERM
process.on('SIGTERM', () => {
console.log('\n\nProcess terminated.');
process.exit(0);
});
}
/**
* Graceful shutdown handler
*/
export function setupGracefulShutdown() {
const gracefulShutdown = (signal) => {
console.log(`\n\nReceived ${signal}. Shutting down gracefully...`);
process.exit(0);
};
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
}