From a8b59926ce1f3f806a8f228e30dffe0f17cd1541 Mon Sep 17 00:00:00 2001 From: raj pandey Date: Wed, 20 Mar 2024 18:32:23 +0530 Subject: [PATCH] Added error logs only and for the api errors showing only the selected fields, removed the error handler and updated the entry migration script --- .../src/utils/asset-folder-create-script.ts | 4 +- .../src/utils/entry-create-script.ts | 18 ++-- .../src/commands/cm/stacks/migration.js | 24 ++--- .../src/utils/error-handler.js | 26 +++-- .../src/utils/error-helper.js | 98 ++++++++++--------- .../src/utils/logger.js | 8 +- .../src/utils/migration-logger.js | 10 +- 7 files changed, 90 insertions(+), 98 deletions(-) diff --git a/packages/contentstack-branches/src/utils/asset-folder-create-script.ts b/packages/contentstack-branches/src/utils/asset-folder-create-script.ts index 30ad3ec1fe..a0c97bfd1c 100644 --- a/packages/contentstack-branches/src/utils/asset-folder-create-script.ts +++ b/packages/contentstack-branches/src/utils/asset-folder-create-script.ts @@ -114,9 +114,9 @@ export function assetFolderCreateScript(contentType) { const createAssetTask = () => { return { - title: 'Create Assets Folder', + title: 'Check and create asset folder in base branch', successTitle: 'Assets folder Created Successfully', - failedTitle: 'Failed to create assets folder', + failedTitle: 'Failed to create assets folder in base branch', task: async () => { try { const baseAssetsFolderCount = await getAssetCount(branch, true); diff --git a/packages/contentstack-branches/src/utils/entry-create-script.ts b/packages/contentstack-branches/src/utils/entry-create-script.ts index df58d050ae..8e17010add 100644 --- a/packages/contentstack-branches/src/utils/entry-create-script.ts +++ b/packages/contentstack-branches/src/utils/entry-create-script.ts @@ -544,21 +544,21 @@ export function entryCreateScript(contentType) { } try { - compareFilteredProperties.length !== 0 && - compareFilteredProperties.forEach(async (entryDetails) => { - if(entryDetails !== undefined){ + if (compareFilteredProperties.length !== 0) { + for (let i = 0; i < compareFilteredProperties.length; i++) { + let entryDetails = compareFilteredProperties[i]; + if (entryDetails !== undefined) { entryDetails = updateAssetDetailsInEntries(entryDetails); - let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }).catch(err => { - throw err - }); - if(createdEntry){ + let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }) + if (createdEntry) { if (flag.references) { await updateReferences(entryDetails, createdEntry, references); } - await updateEntry(createdEntry, entryDetails); + await updateEntry(createdEntry, entryDetails) } } - }); + } + } } catch (error) { throw error; } diff --git a/packages/contentstack-migration/src/commands/cm/stacks/migration.js b/packages/contentstack-migration/src/commands/cm/stacks/migration.js index 293a110512..fd17a7b6f5 100644 --- a/packages/contentstack-migration/src/commands/cm/stacks/migration.js +++ b/packages/contentstack-migration/src/commands/cm/stacks/migration.js @@ -12,6 +12,7 @@ const { Parser } = require('../../../modules'); const { ActionList } = require('../../../actions'); const fs = require('fs'); const chalk = require('chalk'); +const isEmpty = require('lodash/isEmpty'); const { printFlagDeprecation, managementSDKClient, @@ -24,8 +25,6 @@ const { ApiError, SchemaValidator, MigrationError, FieldValidator } = require('. // Utils const { map: _map, constants, safePromise, errorHelper } = require('../../../utils'); -const { success } = require('../../../utils/logger'); - // Properties const { get, set, getMapInstance, resetMapInstance } = _map; const { @@ -134,6 +133,10 @@ class MigrationCommand extends Command { } else { await this.execSingleFile(filePath, mapInstance); } + const errLogPath = `${process.cwd()}/migration-logs`; + if (fs.existsSync(errLogPath) && !isEmpty(`${errLogPath}/error.logs`)) { + this.log(`The log has been stored at: `, errLogPath); + } } async execSingleFile(filePath, mapInstance) { @@ -160,21 +163,11 @@ class MigrationCommand extends Command { const listr = new Listr(tasks); - await listr.run().catch((error) => { - this.handleErrors(error); - // When the process is child, send error message to parent - if (process.send) process.send({ errorOccurred: true }); - }); + await listr.run(); requests.splice(0, requests.length); } catch (error) { - // errorHandler(null, null, null, error) - if (error.message) { - this.log(error.message); - } else if (error.errorMessage) { - this.log(error.errorMessage); - } else { - this.log(error); - } + errorHelper(error, filePath); + if (process.send) process.send({ errorOccurred: true }); } } @@ -186,7 +179,6 @@ class MigrationCommand extends Command { for (const element of files) { const file = element; if (extname(file) === '.js') { - success(chalk`{white Executing file:} {grey {bold ${file}}}`); // eslint-disable-next-line no-await-in-loop await this.execSingleFile(pathValidator(resolve(filePath, file)), mapInstance); } diff --git a/packages/contentstack-migration/src/utils/error-handler.js b/packages/contentstack-migration/src/utils/error-handler.js index e79710354e..9d42b3693f 100644 --- a/packages/contentstack-migration/src/utils/error-handler.js +++ b/packages/contentstack-migration/src/utils/error-handler.js @@ -1,21 +1,19 @@ 'use strict'; -const { error } = require('./logger'); const { errorMessageHandler } = require('./constants'); module.exports = (data, type, method, err) => { - if (data && type && method) { - error(`Error occurred while ${errorMessageHandler[method]} ${type}: ${data}.`); - } - - if (err.errorMessage) { - error(err.errorMessage); - } - if (err instanceof Error && err && err.message && err.stack) { - error(err.message); - // error(err.stack) - } else { - error(err); - } + // NOTE: Commenting this code as the API errors are handled through error-helper.js also + // if (data && type && method) { + // logger.log('error', { error: `Error occurred while ${errorMessageHandler[method]} ${type}: ${data}.` }); + // } + // if (err.errorMessage) { + // logger.log('error', { errorAPI: err.errorMessage }); + // } + // if (err instanceof Error && err && err.message && err.stack) { + // logger.log('error', { error: err.message }); + // } else { + // logger.log('error', { error: err }); + // } // throw new Error(err); }; diff --git a/packages/contentstack-migration/src/utils/error-helper.js b/packages/contentstack-migration/src/utils/error-helper.js index c1832ec15c..6117725f68 100644 --- a/packages/contentstack-migration/src/utils/error-helper.js +++ b/packages/contentstack-migration/src/utils/error-helper.js @@ -4,7 +4,6 @@ const chalk = require('chalk'); const isEmpty = require('lodash/isEmpty'); const MigrationLogger = require('./migration-logger'); const fs = require('fs'); -const logger = new MigrationLogger(process.cwd()); const { readFile } = require('./fs-helper'); const groupBy = require('./group-by'); @@ -25,56 +24,67 @@ function removeSpecialCharacter(str) { return str.replace(/\u001b\[\d+m/g, ''); } -module.exports = (errors) => { +module.exports = (errors, filePath) => { + const logger = new MigrationLogger(process.cwd()); + const errorsByFile = groupBy(errors, 'file'); const messages = []; - for (const file of keys(errorsByFile)) { - const errorLogs = {}; - errorLogs[file] = {}; - const fileContents = readFile(file); - const highlightedCode = highlight(fileContents, { linenos: true }); - const lines = highlightedCode.split('\n'); + if (filePath) { + if (errors.request) { + errors.data = errors.request?.data; + delete errors.request; + } + if (errors.message) { + delete errors.message; + } + logger.log('error', { [filePath]: errors }); + } else { + for (const file of keys(errorsByFile)) { + const errorLogs = {}; + errorLogs[file] = {}; + const fileContents = readFile(file); + const highlightedCode = highlight(fileContents, { linenos: true }); + const lines = highlightedCode.split('\n'); - const fileErrorsMessage = chalk`{red Errors in ${file}}\n\n`; - errorLogs[file].fileErrorsMessage = fileErrorsMessage.replace(/\u001b\[\d+m/g, ''); - const errorMessages = errorsByFile[file] - .map((error) => { - const callsite = error.meta.callsite; - const context = 2; - let { before, line, after } = getLineWithContext(lines, callsite.line, context); + const fileErrorsMessage = chalk`{red Errors in ${file}}\n\n`; + errorLogs[file].fileErrorsMessage = fileErrorsMessage.replace(/\u001b\[\d+m/g, ''); + const errorMessages = errorsByFile[file] + .map((error) => { + const callsite = error.meta.callsite; + const context = 2; + let { before, line, after } = getLineWithContext(lines, callsite.line, context); - const beforeLines = before.map((_line) => chalk`${_line}\n`); - const afterLines = after.map((_line) => chalk`${_line}\n`); - const highlightedLine = chalk`{bold ${line}}\n`; + const beforeLines = before.map((_line) => chalk`${_line}\n`); + const afterLines = after.map((_line) => chalk`${_line}\n`); + const highlightedLine = chalk`{bold ${line}}\n`; - before = removeSpecialCharacter(before.join('\n')); - after = removeSpecialCharacter(after.join('\n')); - line = removeSpecialCharacter(line); - errorLogs[file].lines = { before, line, after }; + before = removeSpecialCharacter(before.join('\n')); + after = removeSpecialCharacter(after.join('\n')); + line = removeSpecialCharacter(line); + errorLogs[file].lines = { before, line, after }; - const formattedCode = beforeLines + highlightedLine + afterLines; - if (error.payload.apiError) { - errorLogs[file].apiError = true; - errorLogs[file].errorCode = error.payload.apiError.errorCode; - errorLogs[file].errors = error.payload.apiError.errors; - } - if (error.message && !error.payload.apiError) { - errorLogs[file].apiError = false; - errorLogs[file].error = error.message; - } - }) - .join('\n'); + const formattedCode = beforeLines + highlightedLine + afterLines; + if (error.payload?.apiError) { + errorLogs[file].apiError = true; + errorLogs[file].errorCode = error.payload.apiError.errorCode; + errorLogs[file].errors = error.payload.apiError.errors; + } + if (error.message && !error.payload.apiError) { + errorLogs[file].apiError = false; + errorLogs[file].error = error.message; + } + }) + .join('\n'); - messages.push(`${fileErrorsMessage}${errorMessages}`); - logger.log('error', errorLogs); - } - if (isEmpty(messages) && errors !== undefined && isEmpty(errorsByFile)) { - console.error('Migration error---', errors); - logger.log('error', errors); - } else { - logger.log('error', { error: messages.join('\n') }); - console.log(messages.join('\n')); + messages.push(`${fileErrorsMessage}${errorMessages}`); + logger.log('error', errorLogs); + } + if (isEmpty(messages) && errors !== undefined && isEmpty(errorsByFile)) { + logger.log('error', errors); + } else { + logger.log('error', { error: messages.join('\n') }); + } } // eslint-disable-next-line - console.log(chalk`{bold.red Migration unsuccessful}`); + // console.log(chalk`{bold.red Migration unsuccessful}`); }; diff --git a/packages/contentstack-migration/src/utils/logger.js b/packages/contentstack-migration/src/utils/logger.js index f2c1ea57dc..d6f96953c1 100644 --- a/packages/contentstack-migration/src/utils/logger.js +++ b/packages/contentstack-migration/src/utils/logger.js @@ -36,7 +36,7 @@ function init(logFileName) { const logPath = pathValidator(join(logsDir, logFileName + '.log')); const logger = createLogger({ format: combine(colorize(), label({ label: 'Migration' }), customFormat), - transports: [new transports.File({ filename: logPath }), new transports.Console()], + transports: [new transports.File({ filename: logPath })], }); let args; @@ -54,9 +54,9 @@ function init(logFileName) { logString && logger.log('warn', logString); }, error: function () { - args = slice.call(arguments); - logString = getString(args); - logString && logger.log('error', logString); + // args = slice.call(arguments); + // logString = getString(args); + // logString && logger.log('error', logString); }, debug: function () { args = slice.call(arguments); diff --git a/packages/contentstack-migration/src/utils/migration-logger.js b/packages/contentstack-migration/src/utils/migration-logger.js index 76068082f7..0417f1e637 100644 --- a/packages/contentstack-migration/src/utils/migration-logger.js +++ b/packages/contentstack-migration/src/utils/migration-logger.js @@ -4,25 +4,17 @@ module.exports = class MigrationLogger { constructor(filePath) { this.filePath = path.join(filePath, 'migration-logs'); this.logger = winston.createLogger({ - levels: { info: 1, error: 2 }, + levels: { error: 1 }, transports: [ new winston.transports.File({ level: 'error', filename: path.join(this.filePath, 'error.logs'), format: winston.format.combine(winston.format.timestamp(), winston.format.json()), }), - new winston.transports.File({ - level: 'info', - filename: path.join(this.filePath, 'info.logs'), - format: winston.format.combine(winston.format.timestamp(), winston.format.json()), - }), ], }); } log(level, message) { - if (level === 'info') { - this.logger.log('info', message); - } this.logger.log('error', message); } };