test: trivial refinements
[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 defaultErrorParams = {
20 throwError: true,
21 consoleOut: false
22 } satisfies HandleErrorParams<EmptyObject>
23
24 export const handleUncaughtException = (): void => {
25 process.on('uncaughtException', (error: Error) => {
26 console.error(chalk.red('Uncaught exception: '), error)
27 })
28 }
29
30 export const handleUnhandledRejection = (): void => {
31 process.on('unhandledRejection', (reason: unknown) => {
32 console.error(chalk.red('Unhandled rejection: '), reason)
33 })
34 }
35
36 export const handleFileException = (
37 file: string,
38 fileType: FileType,
39 error: NodeJS.ErrnoException,
40 logPrefix: string,
41 params: HandleErrorParams<EmptyObject> = defaultErrorParams
42 ): void => {
43 params = setDefaultErrorParams(params)
44 const prefix = isNotEmptyString(logPrefix) ? `${logPrefix} ` : ''
45 let logMsg: string
46 switch (error.code) {
47 case 'ENOENT':
48 logMsg = `${fileType} file ${file} not found:`
49 break
50 case 'EEXIST':
51 logMsg = `${fileType} file ${file} already exists:`
52 break
53 case 'EACCES':
54 logMsg = `${fileType} file ${file} access denied:`
55 break
56 case 'EPERM':
57 logMsg = `${fileType} file ${file} permission denied:`
58 break
59 default:
60 logMsg = `${fileType} file ${file} error:`
61 }
62 if (params.consoleOut === true) {
63 logMsg = `${logMsg} `
64 if (params.throwError === true) {
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 === true) {
71 logger.error(`${prefix}${logMsg}`, error)
72 } else {
73 logger.warn(`${prefix}${logMsg}`, error)
74 }
75 }
76 if (params.throwError === true) {
77 throw error
78 }
79 }
80
81 export const handleSendMessageError = (
82 chargingStation: ChargingStation,
83 commandName: RequestCommand | IncomingRequestCommand,
84 messageType: MessageType,
85 error: Error,
86 params: HandleErrorParams<EmptyObject> = {
87 throwError: false,
88 consoleOut: false
89 }
90 ): void => {
91 params = setDefaultErrorParams(params, { throwError: false, consoleOut: false })
92 logger.error(
93 `${chargingStation.logPrefix()} Send ${getMessageTypeString(messageType)} command '${commandName}' error:`,
94 error
95 )
96 if (params.throwError === true) {
97 throw error
98 }
99 }
100
101 export const setDefaultErrorParams = <T extends JsonType>(
102 params: HandleErrorParams<T>,
103 defaultParams: HandleErrorParams<T> = defaultErrorParams
104 ): HandleErrorParams<T> => {
105 return { ...defaultParams, ...params }
106 }