Commit | Line | Data |
---|---|---|
66a7748d | 1 | import process from 'node:process' |
36adaf06 | 2 | |
66a7748d | 3 | import chalk from 'chalk' |
51022aa0 | 4 | |
66a7748d | 5 | import type { ChargingStation } from '../charging-station/index.js' |
7671fa0b JB |
6 | import type { |
7 | EmptyObject, | |
8 | FileType, | |
9 | HandleErrorParams, | |
10 | IncomingRequestCommand, | |
7b5dbe91 | 11 | JsonType, |
66a7748d JB |
12 | RequestCommand |
13 | } from '../types/index.js' | |
4c3f6c20 JB |
14 | import { logger } from './Logger.js' |
15 | import { isNotEmptyString } from './Utils.js' | |
51022aa0 | 16 | |
1c34e5f9 JB |
17 | const defaultErrorParams = { |
18 | throwError: true, | |
66a7748d | 19 | consoleOut: false |
97608fbd | 20 | } satisfies HandleErrorParams<EmptyObject> |
1c34e5f9 | 21 | |
fa5995d6 JB |
22 | export 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 |
28 | export 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 |
34 | export 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 |
79 | export const handleSendMessageError = ( |
80 | chargingStation: ChargingStation, | |
81 | commandName: RequestCommand | IncomingRequestCommand, | |
82 | error: Error, | |
66a7748d | 83 | params: HandleErrorParams<EmptyObject> = { throwError: false, consoleOut: false } |
fa5995d6 | 84 | ): void => { |
66a7748d JB |
85 | setDefaultErrorParams(params, { throwError: false, consoleOut: false }) |
86 | logger.error(`${chargingStation.logPrefix()} Request command '${commandName}' error:`, error) | |
5199f9fd | 87 | if (params.throwError === true) { |
66a7748d | 88 | throw error |
7b5dbe91 | 89 | } |
66a7748d | 90 | } |
fa5995d6 JB |
91 | |
92 | export const setDefaultErrorParams = <T extends JsonType>( | |
93 | params: HandleErrorParams<T>, | |
66a7748d | 94 | defaultParams: HandleErrorParams<T> = defaultErrorParams |
fa5995d6 | 95 | ): HandleErrorParams<T> => { |
66a7748d JB |
96 | params = { ...defaultParams, ...params } |
97 | return params | |
98 | } |