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