Skip to content

Commit

Permalink
Added error logs only and for the api errors showing only the selecte…
Browse files Browse the repository at this point in the history
…d fields, removed the error handler and updated the entry migration script
  • Loading branch information
cs-raj committed Mar 20, 2024
1 parent 8bf0fc8 commit a8b5992
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions packages/contentstack-branches/src/utils/entry-create-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 });
}
}

Expand All @@ -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);
}
Expand Down
26 changes: 12 additions & 14 deletions packages/contentstack-migration/src/utils/error-handler.js
Original file line number Diff line number Diff line change
@@ -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);
};
98 changes: 54 additions & 44 deletions packages/contentstack-migration/src/utils/error-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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}`);
};
8 changes: 4 additions & 4 deletions packages/contentstack-migration/src/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
10 changes: 1 addition & 9 deletions packages/contentstack-migration/src/utils/migration-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

0 comments on commit a8b5992

Please sign in to comment.