Commit | Line | Data |
---|---|---|
e0a50bcd | 1 | import { HandleErrorParams } from '../types/Error'; |
8eac9a09 | 2 | import chalk from 'chalk'; |
9f2e3130 | 3 | import logger from './Logger'; |
23132a44 JB |
4 | |
5 | export default class FileUtils { | |
e0a50bcd JB |
6 | static handleFileException(logPrefix: string, fileType: string, filePath: string, error: NodeJS.ErrnoException, |
7 | params: HandleErrorParams = { throwError: true, consoleOut: false }): void { | |
23132a44 JB |
8 | const prefix = logPrefix.length !== 0 ? logPrefix + ' ' : ''; |
9 | if (error.code === 'ENOENT') { | |
e0a50bcd | 10 | if (params?.consoleOut) { |
e4362ed7 | 11 | console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' not found: '), error); |
23132a44 | 12 | } else { |
9f2e3130 | 13 | logger.warn(prefix + fileType + ' file ' + filePath + ' not found: %j', error); |
23132a44 | 14 | } |
72f041bd | 15 | } else if (error.code === 'EEXIST') { |
e0a50bcd | 16 | if (params?.consoleOut) { |
e4362ed7 | 17 | console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' already exists: '), error); |
72f041bd | 18 | } else { |
9f2e3130 | 19 | logger.warn(prefix + fileType + ' file ' + filePath + ' already exists: %j', error); |
72f041bd JB |
20 | } |
21 | } else if (error.code === 'EACCES') { | |
e0a50bcd | 22 | if (params?.consoleOut) { |
e4362ed7 | 23 | console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' access denied: '), error); |
72f041bd | 24 | } else { |
9f2e3130 | 25 | logger.warn(prefix + fileType + ' file ' + filePath + ' access denied: %j', error); |
72f041bd | 26 | } |
23132a44 | 27 | } else { |
e0a50bcd | 28 | if (params?.consoleOut) { |
e4362ed7 | 29 | console.warn(chalk.green(prefix) + chalk.yellow(fileType + ' file ' + filePath + ' error: '), error); |
23132a44 | 30 | } else { |
9f2e3130 | 31 | logger.warn(prefix + fileType + ' file ' + filePath + ' error: %j', error); |
23132a44 | 32 | } |
e0a50bcd JB |
33 | if (params?.throwError) { |
34 | throw error; | |
35 | } | |
23132a44 JB |
36 | } |
37 | } | |
38 | } |