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