refactor: cleanup default params in error handlers
[e-mobility-charging-stations-simulator.git] / src / utils / ErrorUtils.ts
1 import process from 'node:process'
2
3 import chalk from 'chalk'
4
5 import type { ChargingStation } from '../charging-station/index.js'
6 import { getMessageTypeString } from '../charging-station/ocpp/OCPPServiceUtils.js'
7 import type {
8 EmptyObject,
9 FileType,
10 HandleErrorParams,
11 IncomingRequestCommand,
12 JsonType,
13 MessageType,
14 RequestCommand
15 } from '../types/index.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 throwError: true,
43 consoleOut: false
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: RequestCommand | IncomingRequestCommand,
87 messageType: MessageType,
88 error: Error,
89 params?: HandleErrorParams<EmptyObject>
90 ): void => {
91 params = {
92 ...{
93 throwError: false,
94 consoleOut: 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 throwError: true,
116 consoleOut: false
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 }