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