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