diff --git a/src/common/util/logger.ts b/src/common/util/logger.ts index 4d01d596..5511292e 100644 --- a/src/common/util/logger.ts +++ b/src/common/util/logger.ts @@ -14,8 +14,8 @@ export const logger = winston.createLogger({ align(), printf( (info: any) => - `[${info.timestamp}] ${info.level}: ${info.message} -${info.stack || ''}` + `[${info.timestamp}] ${info.stack || `${info.level}: ${info.message}`} +` ) ), transports: [new winston.transports.Console()], diff --git a/src/common/util/yaml.ts b/src/common/util/yaml.ts index beaedd71..036fb445 100644 --- a/src/common/util/yaml.ts +++ b/src/common/util/yaml.ts @@ -2,25 +2,36 @@ import * as fs from 'fs/promises'; import * as path from 'path'; import * as YAML from 'js-yaml'; +import {ERRORS} from '@grnsft/if-core/utils'; + +const {ReadFileError, WriteFileError} = ERRORS; /** * Reads and parses `yaml` file to object. */ export const openYamlFileAsObject = async (filePath: string): Promise => { - const yamlFileBuffer = await fs.readFile(filePath, 'utf8'); + try { + const yamlFileBuffer = await fs.readFile(filePath, 'utf8'); - return YAML.load(yamlFileBuffer) as T; + return YAML.load(yamlFileBuffer) as T; + } catch (error: any) { + throw new ReadFileError(error.message); + } }; /** * Saves given `yaml` dump as a file. */ export const saveYamlFileAs = async (object: any, pathToFile: string) => { - const dirPath = path.dirname(pathToFile); - await fs.mkdir(dirPath, {recursive: true}); - const yamlString = YAML.dump(object, {noRefs: true}); + try { + const dirPath = path.dirname(pathToFile); + await fs.mkdir(dirPath, {recursive: true}); + const yamlString = YAML.dump(object, {noRefs: true}); - return fs.writeFile(pathToFile, yamlString); + return fs.writeFile(pathToFile, yamlString); + } catch (error: any) { + throw new WriteFileError(error.message); + } }; /**