| 1 | import chalk from 'chalk'; |
| 2 | |
| 3 | import { logger } from './Logger'; |
| 4 | import { Utils } from './Utils'; |
| 5 | import type { ChargingStation } from '../charging-station'; |
| 6 | import type { |
| 7 | EmptyObject, |
| 8 | FileType, |
| 9 | HandleErrorParams, |
| 10 | IncomingRequestCommand, |
| 11 | JsonType, |
| 12 | RequestCommand, |
| 13 | } from '../types'; |
| 14 | |
| 15 | const defaultErrorParams = { |
| 16 | throwError: true, |
| 17 | consoleOut: false, |
| 18 | }; |
| 19 | |
| 20 | export class ErrorUtils { |
| 21 | private constructor() { |
| 22 | // This is intentional |
| 23 | } |
| 24 | |
| 25 | public static handleUncaughtException(): void { |
| 26 | process.on('uncaughtException', (error: Error) => { |
| 27 | console.error(chalk.red('Uncaught exception: '), error); |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | public static handleUnhandledRejection(): void { |
| 32 | process.on('unhandledRejection', (reason: unknown) => { |
| 33 | console.error(chalk.red('Unhandled rejection: '), reason); |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | public static handleFileException( |
| 38 | file: string, |
| 39 | fileType: FileType, |
| 40 | error: NodeJS.ErrnoException, |
| 41 | logPrefix: string, |
| 42 | params: HandleErrorParams<EmptyObject> = defaultErrorParams |
| 43 | ): void { |
| 44 | ErrorUtils.setDefaultErrorParams(params); |
| 45 | const prefix = Utils.isNotEmptyString(logPrefix) ? `${logPrefix} ` : ''; |
| 46 | let logMsg: string; |
| 47 | switch (error.code) { |
| 48 | case 'ENOENT': |
| 49 | logMsg = `${fileType} file ${file} not found:`; |
| 50 | break; |
| 51 | case 'EEXIST': |
| 52 | logMsg = `${fileType} file ${file} already exists:`; |
| 53 | break; |
| 54 | case 'EACCES': |
| 55 | logMsg = `${fileType} file ${file} access denied:`; |
| 56 | break; |
| 57 | case 'EPERM': |
| 58 | logMsg = `${fileType} file ${file} permission denied:`; |
| 59 | break; |
| 60 | default: |
| 61 | logMsg = `${fileType} file ${file} error:`; |
| 62 | } |
| 63 | if (params?.consoleOut === true) { |
| 64 | if (params?.throwError) { |
| 65 | console.error(`${chalk.green(prefix)}${chalk.red(`${logMsg} `)}`, error); |
| 66 | } else { |
| 67 | console.warn(`${chalk.green(prefix)}${chalk.yellow(`${logMsg} `)}`, error); |
| 68 | } |
| 69 | } else if (params?.consoleOut === false) { |
| 70 | if (params?.throwError) { |
| 71 | logger.error(`${prefix}${logMsg}`, error); |
| 72 | } else { |
| 73 | logger.warn(`${prefix}${logMsg}`, error); |
| 74 | } |
| 75 | } |
| 76 | if (params?.throwError) { |
| 77 | throw error; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public static handleSendMessageError( |
| 82 | chargingStation: ChargingStation, |
| 83 | commandName: RequestCommand | IncomingRequestCommand, |
| 84 | error: Error, |
| 85 | params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false } |
| 86 | ): void { |
| 87 | ErrorUtils.setDefaultErrorParams(params, { throwError: false, consoleOut: false }); |
| 88 | logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error); |
| 89 | if (params?.throwError === true) { |
| 90 | throw error; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public static setDefaultErrorParams<T extends JsonType>( |
| 95 | params: HandleErrorParams<T>, |
| 96 | defaultParams: HandleErrorParams<T> = defaultErrorParams |
| 97 | ): HandleErrorParams<T> { |
| 98 | params = { ...defaultParams, ...params }; |
| 99 | return params; |
| 100 | } |
| 101 | } |