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