@typecad/jlcpcb-parts
Version:
Intelligent fuzzy search for JLCPCB electrical components with CLI interface
82 lines • 2.57 kB
JavaScript
import { ErrorHandler } from './ErrorHandler.js';
/**
* Class for handling global unhandled exceptions and rejections
*/
export class GlobalErrorHandler {
static isInitialized = false;
static logger;
/**
* Initializes the global error handler
* @param logger - Logger instance to use
*/
static initialize(logger) {
if (this.isInitialized) {
return;
}
this.logger = logger;
// Handle uncaught exceptions
process.on('uncaughtException', (error) => {
this.handleUncaughtException(error);
});
// Handle unhandled rejections
process.on('unhandledRejection', (reason) => {
this.handleUnhandledRejection(reason);
});
// Handle warnings
process.on('warning', (warning) => {
this.handleWarning(warning);
});
// Handle exit
process.on('exit', (code) => {
this.handleExit(code);
});
this.isInitialized = true;
logger.info('Global error handler initialized');
}
/**
* Handles an uncaught exception
* @param error - The uncaught exception
*/
static handleUncaughtException(error) {
this.logger.error('Uncaught exception', error);
// Use ErrorHandler to get a user-friendly error message
const errorMessage = ErrorHandler.handleError(error);
console.error(`\nFatal error: ${errorMessage}`);
// Exit with error code
process.exit(1);
}
/**
* Handles an unhandled rejection
* @param reason - The reason for the rejection
*/
static handleUnhandledRejection(reason) {
let error;
if (reason instanceof Error) {
error = reason;
}
else {
error = new Error(`Unhandled rejection: ${String(reason)}`);
}
this.logger.error('Unhandled rejection', error);
// Use ErrorHandler to get a user-friendly error message
const errorMessage = ErrorHandler.handleError(error);
console.error(`\nFatal error: ${errorMessage}`);
// Exit with error code
process.exit(1);
}
/**
* Handles a warning
* @param warning - The warning
*/
static handleWarning(warning) {
this.logger.warn('Warning', warning);
}
/**
* Handles process exit
* @param code - The exit code
*/
static handleExit(code) {
this.logger.info(`Process exiting with code ${code}`);
}
}
//# sourceMappingURL=GlobalErrorHandler.js.map