1 import process from
'node:process'
3 import chalk from
'chalk'
5 import type { ChargingStation
} from
'../charging-station/index.js'
6 import { getMessageTypeString
} from
'../charging-station/ocpp/OCPPServiceUtils.js'
11 IncomingRequestCommand
,
15 } from
'../types/index.js'
16 import { logger
} from
'./Logger.js'
17 import { isNotEmptyString
} from
'./Utils.js'
19 const moduleName
= 'ErrorUtils'
21 const defaultErrorParams
= {
24 } satisfies HandleErrorParams
<EmptyObject
>
26 export const handleUncaughtException
= (): void => {
27 process
.on('uncaughtException', (error
: Error) => {
28 console
.error(chalk
.red('Uncaught exception: '), error
)
32 export const handleUnhandledRejection
= (): void => {
33 process
.on('unhandledRejection', (reason
: unknown
) => {
34 console
.error(chalk
.red('Unhandled rejection: '), reason
)
38 export const handleFileException
= (
41 error
: NodeJS
.ErrnoException
,
43 params
: HandleErrorParams
<EmptyObject
> = defaultErrorParams
45 params
= setDefaultErrorParams(params
)
46 const prefix
= isNotEmptyString(logPrefix
) ? `${logPrefix} ` : ''
50 logMsg
= `${fileType} file ${file} not found:`
53 logMsg
= `${fileType} file ${file} already exists:`
56 logMsg
= `${fileType} file ${file} access denied:`
59 logMsg
= `${fileType} file ${file} permission denied:`
62 logMsg
= `${fileType} file ${file} error:`
64 if (params
.consoleOut
=== true) {
66 if (params
.throwError
=== true) {
67 console
.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error
)
69 console
.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error
)
71 } else if (params
.consoleOut
=== false) {
72 if (params
.throwError
=== true) {
73 logger
.error(`${prefix}${logMsg}`, error
)
75 logger
.warn(`${prefix}${logMsg}`, error
)
78 if (params
.throwError
=== true) {
83 export const handleSendMessageError
= (
84 chargingStation
: ChargingStation
,
85 commandName
: RequestCommand
| IncomingRequestCommand
,
86 messageType
: MessageType
,
88 params
: HandleErrorParams
<EmptyObject
> = {
93 params
= setDefaultErrorParams(params
, { throwError
: false, consoleOut
: false })
95 `${chargingStation.logPrefix()} ${moduleName}.handleSendMessageError: Send ${getMessageTypeString(messageType)} command '${commandName}' error:`,
98 if (params
.throwError
=== true) {
103 export const handleIncomingRequestError
= <T
extends JsonType
>(
104 chargingStation
: ChargingStation
,
105 commandName
: IncomingRequestCommand
,
107 params
: HandleErrorParams
<T
> = { throwError
: true, consoleOut
: false }
108 ): T
| undefined => {
109 params
= setDefaultErrorParams(params
)
111 `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`,
114 if (params
.throwError
=== false && params
.errorResponse
!= null) {
115 return params
.errorResponse
117 if (params
.throwError
=== true && params
.errorResponse
== null) {
120 if (params
.throwError
=== true && params
.errorResponse
!= null) {
121 return params
.errorResponse
125 export const setDefaultErrorParams
= <T
extends JsonType
>(
126 params
: HandleErrorParams
<T
>,
127 defaultParams
: HandleErrorParams
<T
> = defaultErrorParams
128 ): HandleErrorParams
<T
> => {
129 return { ...defaultParams
, ...params
}