| 1 | import chalk from 'chalk' |
| 2 | import process from 'node:process' |
| 3 | |
| 4 | import type { ChargingStation } from '../charging-station/index.js' |
| 5 | import type { |
| 6 | EmptyObject, |
| 7 | FileType, |
| 8 | HandleErrorParams, |
| 9 | IncomingRequestCommand, |
| 10 | JsonType, |
| 11 | MessageType, |
| 12 | RequestCommand, |
| 13 | } from '../types/index.js' |
| 14 | |
| 15 | import { getMessageTypeString } from '../charging-station/ocpp/OCPPServiceUtils.js' |
| 16 | import { logger } from './Logger.js' |
| 17 | import { isNotEmptyString } from './Utils.js' |
| 18 | |
| 19 | const moduleName = 'ErrorUtils' |
| 20 | |
| 21 | export const handleUncaughtException = (): void => { |
| 22 | process.on('uncaughtException', (error: Error) => { |
| 23 | console.error(chalk.red('Uncaught exception: '), error) |
| 24 | }) |
| 25 | } |
| 26 | |
| 27 | export const handleUnhandledRejection = (): void => { |
| 28 | process.on('unhandledRejection', (reason: unknown) => { |
| 29 | console.error(chalk.red('Unhandled rejection: '), reason) |
| 30 | }) |
| 31 | } |
| 32 | |
| 33 | export const handleFileException = ( |
| 34 | file: string, |
| 35 | fileType: FileType, |
| 36 | error: NodeJS.ErrnoException, |
| 37 | logPrefix: string, |
| 38 | params?: HandleErrorParams<EmptyObject> |
| 39 | ): void => { |
| 40 | params = { |
| 41 | ...{ |
| 42 | consoleOut: false, |
| 43 | throwError: true, |
| 44 | }, |
| 45 | ...params, |
| 46 | } |
| 47 | const prefix = isNotEmptyString(logPrefix) ? `${logPrefix} ` : '' |
| 48 | let logMsg: string |
| 49 | switch (error.code) { |
| 50 | case 'ENOENT': |
| 51 | logMsg = `${fileType} file ${file} not found:` |
| 52 | break |
| 53 | case 'EEXIST': |
| 54 | logMsg = `${fileType} file ${file} already exists:` |
| 55 | break |
| 56 | case 'EACCES': |
| 57 | logMsg = `${fileType} file ${file} access denied:` |
| 58 | break |
| 59 | case 'EPERM': |
| 60 | logMsg = `${fileType} file ${file} permission denied:` |
| 61 | break |
| 62 | default: |
| 63 | logMsg = `${fileType} file ${file} error:` |
| 64 | } |
| 65 | if (params.consoleOut === true) { |
| 66 | logMsg = `${logMsg} ` |
| 67 | if (params.throwError === true) { |
| 68 | console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error) |
| 69 | } else { |
| 70 | console.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error) |
| 71 | } |
| 72 | } else if (params.consoleOut === false) { |
| 73 | if (params.throwError === true) { |
| 74 | logger.error(`${prefix}${logMsg}`, error) |
| 75 | } else { |
| 76 | logger.warn(`${prefix}${logMsg}`, error) |
| 77 | } |
| 78 | } |
| 79 | if (params.throwError === true) { |
| 80 | throw error |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | export const handleSendMessageError = ( |
| 85 | chargingStation: ChargingStation, |
| 86 | commandName: IncomingRequestCommand | RequestCommand, |
| 87 | messageType: MessageType, |
| 88 | error: Error, |
| 89 | params?: HandleErrorParams<EmptyObject> |
| 90 | ): void => { |
| 91 | params = { |
| 92 | ...{ |
| 93 | consoleOut: false, |
| 94 | throwError: false, |
| 95 | }, |
| 96 | ...params, |
| 97 | } |
| 98 | logger.error( |
| 99 | `${chargingStation.logPrefix()} ${moduleName}.handleSendMessageError: Send ${getMessageTypeString(messageType)} command '${commandName}' error:`, |
| 100 | error |
| 101 | ) |
| 102 | if (params.throwError === true) { |
| 103 | throw error |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | export const handleIncomingRequestError = <T extends JsonType>( |
| 108 | chargingStation: ChargingStation, |
| 109 | commandName: IncomingRequestCommand, |
| 110 | error: Error, |
| 111 | params?: HandleErrorParams<T> |
| 112 | ): T | undefined => { |
| 113 | params = { |
| 114 | ...{ |
| 115 | consoleOut: false, |
| 116 | throwError: true, |
| 117 | }, |
| 118 | ...params, |
| 119 | } |
| 120 | logger.error( |
| 121 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, |
| 122 | error |
| 123 | ) |
| 124 | if (params.throwError === false && params.errorResponse != null) { |
| 125 | return params.errorResponse |
| 126 | } |
| 127 | if (params.throwError === true && params.errorResponse == null) { |
| 128 | throw error |
| 129 | } |
| 130 | if (params.throwError === true && params.errorResponse != null) { |
| 131 | return params.errorResponse |
| 132 | } |
| 133 | } |