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' |
436569b1 | 6 | import { getMessageTypeString } from '../charging-station/ocpp/OCPPServiceUtils.js' |
7671fa0b JB |
7 | import 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 |
16 | import { logger } from './Logger.js' |
17 | import { isNotEmptyString } from './Utils.js' | |
51022aa0 | 18 | |
2d4928a7 JB |
19 | const moduleName = 'ErrorUtils' |
20 | ||
1c34e5f9 JB |
21 | const defaultErrorParams = { |
22 | throwError: true, | |
66a7748d | 23 | consoleOut: false |
97608fbd | 24 | } satisfies HandleErrorParams<EmptyObject> |
1c34e5f9 | 25 | |
fa5995d6 JB |
26 | export const handleUncaughtException = (): void => { |
27 | process.on('uncaughtException', (error: Error) => { | |
66a7748d JB |
28 | console.error(chalk.red('Uncaught exception: '), error) |
29 | }) | |
30 | } | |
51022aa0 | 31 | |
fa5995d6 JB |
32 | export const handleUnhandledRejection = (): void => { |
33 | process.on('unhandledRejection', (reason: unknown) => { | |
66a7748d JB |
34 | console.error(chalk.red('Unhandled rejection: '), reason) |
35 | }) | |
36 | } | |
7671fa0b | 37 | |
fa5995d6 JB |
38 | export const handleFileException = ( |
39 | file: string, | |
40 | fileType: FileType, | |
41 | error: NodeJS.ErrnoException, | |
42 | logPrefix: string, | |
66a7748d | 43 | params: HandleErrorParams<EmptyObject> = defaultErrorParams |
fa5995d6 | 44 | ): void => { |
30695dcf | 45 | params = setDefaultErrorParams(params) |
66a7748d JB |
46 | const prefix = isNotEmptyString(logPrefix) ? `${logPrefix} ` : '' |
47 | let logMsg: string | |
fa5995d6 JB |
48 | switch (error.code) { |
49 | case 'ENOENT': | |
66a7748d JB |
50 | logMsg = `${fileType} file ${file} not found:` |
51 | break | |
fa5995d6 | 52 | case 'EEXIST': |
66a7748d JB |
53 | logMsg = `${fileType} file ${file} already exists:` |
54 | break | |
fa5995d6 | 55 | case 'EACCES': |
66a7748d JB |
56 | logMsg = `${fileType} file ${file} access denied:` |
57 | break | |
fa5995d6 | 58 | case 'EPERM': |
66a7748d JB |
59 | logMsg = `${fileType} file ${file} permission denied:` |
60 | break | |
fa5995d6 | 61 | default: |
66a7748d | 62 | logMsg = `${fileType} file ${file} error:` |
7671fa0b | 63 | } |
5199f9fd | 64 | if (params.consoleOut === true) { |
66a7748d | 65 | logMsg = `${logMsg} ` |
5199f9fd | 66 | if (params.throwError === true) { |
66a7748d | 67 | console.error(`${chalk.green(prefix)}${chalk.red(logMsg)}`, error) |
fa5995d6 | 68 | } else { |
66a7748d | 69 | console.warn(`${chalk.green(prefix)}${chalk.yellow(logMsg)}`, error) |
51022aa0 | 70 | } |
5199f9fd JB |
71 | } else if (params.consoleOut === false) { |
72 | if (params.throwError === true) { | |
66a7748d | 73 | logger.error(`${prefix}${logMsg}`, error) |
fa5995d6 | 74 | } else { |
66a7748d | 75 | logger.warn(`${prefix}${logMsg}`, error) |
51022aa0 JB |
76 | } |
77 | } | |
5199f9fd | 78 | if (params.throwError === true) { |
66a7748d | 79 | throw error |
7671fa0b | 80 | } |
66a7748d | 81 | } |
7b5dbe91 | 82 | |
fa5995d6 JB |
83 | export const handleSendMessageError = ( |
84 | chargingStation: ChargingStation, | |
85 | commandName: RequestCommand | IncomingRequestCommand, | |
436569b1 | 86 | messageType: MessageType, |
fa5995d6 | 87 | error: Error, |
48847bc0 JB |
88 | params: HandleErrorParams<EmptyObject> = { |
89 | throwError: false, | |
90 | consoleOut: false | |
91 | } | |
fa5995d6 | 92 | ): void => { |
30695dcf | 93 | params = setDefaultErrorParams(params, { throwError: false, consoleOut: false }) |
436569b1 | 94 | logger.error( |
2d4928a7 | 95 | `${chargingStation.logPrefix()} ${moduleName}.handleSendMessageError: Send ${getMessageTypeString(messageType)} command '${commandName}' error:`, |
436569b1 JB |
96 | error |
97 | ) | |
5199f9fd | 98 | if (params.throwError === true) { |
66a7748d | 99 | throw error |
7b5dbe91 | 100 | } |
66a7748d | 101 | } |
fa5995d6 | 102 | |
2d4928a7 JB |
103 | export const handleIncomingRequestError = <T extends JsonType>( |
104 | chargingStation: ChargingStation, | |
105 | commandName: IncomingRequestCommand, | |
106 | error: Error, | |
107 | params: HandleErrorParams<T> = { throwError: true, consoleOut: false } | |
108 | ): T | undefined => { | |
109 | params = setDefaultErrorParams(params) | |
110 | logger.error( | |
111 | `${chargingStation.logPrefix()} ${moduleName}.handleIncomingRequestError: Incoming request command '${commandName}' error:`, | |
112 | error | |
113 | ) | |
114 | if (params.throwError === false && params.errorResponse != null) { | |
115 | return params.errorResponse | |
116 | } | |
117 | if (params.throwError === true && params.errorResponse == null) { | |
118 | throw error | |
119 | } | |
120 | if (params.throwError === true && params.errorResponse != null) { | |
121 | return params.errorResponse | |
122 | } | |
123 | } | |
124 | ||
fa5995d6 JB |
125 | export const setDefaultErrorParams = <T extends JsonType>( |
126 | params: HandleErrorParams<T>, | |
66a7748d | 127 | defaultParams: HandleErrorParams<T> = defaultErrorParams |
fa5995d6 | 128 | ): HandleErrorParams<T> => { |
30695dcf | 129 | return { ...defaultParams, ...params } |
66a7748d | 130 | } |