Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS-44174 - added logger for errors for multiple files errors, version bump #1344

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
204 changes: 143 additions & 61 deletions package-lock.json

Large diffs are not rendered by default.

26 changes: 2 additions & 24 deletions packages/contentstack-branches/src/utils/entry-create-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export function entryCreateScript(contentType) {
const omit = require('lodash/omit');
const compact = require('lodash/compact')
const isPlainObject = require('lodash/isPlainObject');
const {cliux, LoggerService} = require('@contentstack/cli-utilities')
module.exports = async ({ migration, stackSDKInstance, managementAPIClient, config, branch, apiKey }) => {
const keysToRemove = [
'content_type_uid',
Expand Down Expand Up @@ -41,7 +40,6 @@ export function entryCreateScript(contentType) {
let assetRefPath = {};
let downloadedAssets = [];
let parent=[];
let logger;

function getValueByPath(obj, path) {
return path.split('[').reduce((o, key) => o && o[key.replace(/\]$/, '')], obj);
Expand Down Expand Up @@ -470,10 +468,6 @@ export function entryCreateScript(contentType) {
successTitle: 'Entries Created Successfully',
failedTitle: 'Failed to create entries',
task: async () => {
//logger file
if(!fs.existsSync(path.join(filePath, 'entry-migration'))){
logger = new LoggerService(filePath, 'entry-migration');
}

const compareBranchEntries = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
Expand Down Expand Up @@ -521,15 +515,7 @@ export function entryCreateScript(contentType) {
async function updateEntry(entry, entryDetails) {
Object.assign(entry, { ...entryDetails });
await entry.update().catch(err => {
let errorMsg = 'Entry update failed for uid: ' + entry?.uid + ', title: ' + entry?.title + '. ';
if(err?.errors?.title){
errorMsg += 'title'+ err?.errors?.title;
}else if(err?.errors?.entry){
errorMsg += err?.errors?.entry;
}else{
errorMsg += (err?.entry?.errorMessage || err?.errorMessage || err?.message) ?? 'Something went wrong!';
}
logger.error(errorMsg)
throw err;
});
}

Expand Down Expand Up @@ -563,15 +549,7 @@ export function entryCreateScript(contentType) {
if(entryDetails !== undefined){
entryDetails = updateAssetDetailsInEntries(entryDetails);
let createdEntry = await stackSDKInstance.contentType('${contentType}').entry().create({ entry: entryDetails }).catch(err => {
let errorMsg = 'Entry creation failed for contentType: ' + contentType + ', title: ' + entryDetails?.title + '. ';
if(err?.errors?.title){
errorMsg += err?.errors?.title;
}else if(err?.errors?.entry){
errorMsg += err?.errors?.entry;
}else{
errorMsg += (err?.entry?.errorMessage || err?.errorMessage || err?.message) ?? 'Something went wrong!';
}
logger.error(errorMsg)
throw err
});
if(createdEntry){
if (flag.references) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export function entryCreateUpdateScript(contentType) {
const omit = require('lodash/omit');
const compact = require('lodash/compact')
const isPlainObject = require('lodash/isPlainObject');
const {cliux, LoggerService} = require('@contentstack/cli-utilities')
module.exports = async ({ migration, stackSDKInstance, managementAPIClient, config, branch, apiKey }) => {
const keysToRemove = [
'content_type_uid',
Expand Down Expand Up @@ -42,7 +41,6 @@ export function entryCreateUpdateScript(contentType) {
let assetUrlMapper = {};
let assetRefPath = {};
let parent=[];
let logger;

function converter(data) {
let arr = [];
Expand Down Expand Up @@ -485,10 +483,6 @@ export function entryCreateUpdateScript(contentType) {
successMessage: 'Entries Updated Successfully',
failedMessage: 'Failed to update entries',
task: async () => {
//logger file
if(!fs.existsSync(path.join(filePath, 'entry-migration'))){
logger = new LoggerService(filePath, 'entry-migration');
}
let compareBranchEntries = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
.contentType('${contentType}')
Expand Down Expand Up @@ -540,15 +534,7 @@ export function entryCreateUpdateScript(contentType) {
if (entry) {
Object.assign(entry, { ...entryDetails });
await entry.update().catch(err => {
let errorMsg = 'Entry update failed for uid: ' + entry?.uid + ', title: ' + entry?.title + '.';
if(err?.errors?.title){
errorMsg += err?.errors?.title;
}else if(err?.errors?.entry){
errorMsg += err?.errors?.entry;
}else{
errorMsg += (err?.entry?.errorMessage || err?.errorMessage || err?.message) ?? 'Something went wrong!';
}
logger.error(errorMsg)
throw err
});
}
}
Expand Down Expand Up @@ -623,7 +609,7 @@ export function entryCreateUpdateScript(contentType) {
.entry()
.create({ entry: entryDetails })
.catch(err => {
(err?.errorMessage || err?.message) ? err?.errorMessage || err?.message : 'Something went wrong!'
throw err;
})

if(createdEntry){
Expand Down
18 changes: 2 additions & 16 deletions packages/contentstack-branches/src/utils/entry-update-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export function entryUpdateScript(contentType) {
const omit = require('lodash/omit');
const compact = require('lodash/compact')
const isPlainObject = require('lodash/isPlainObject');
const {cliux, LoggerService} = require('@contentstack/cli-utilities')
module.exports = async ({ migration, stackSDKInstance, managementAPIClient, config, branch, apiKey }) => {
const keysToRemove = [
'content_type_uid',
Expand Down Expand Up @@ -42,7 +41,6 @@ export function entryUpdateScript(contentType) {
let assetUrlMapper = {};
let assetRefPath = {};
let parent=[];
let logger;

function converter(data) {
let arr = [];
Expand Down Expand Up @@ -484,10 +482,6 @@ export function entryUpdateScript(contentType) {
successMessage: 'Entries Updated Successfully',
failedMessage: 'Failed to update entries',
task: async () => {
//logger file
if(!fs.existsSync(path.join(filePath, 'entry-migration'))){
logger = new LoggerService(filePath, 'entry-migration');
}

let compareBranchEntries = await managementAPIClient
.stack({ api_key: stackSDKInstance.api_key, branch_uid: compareBranch })
Expand Down Expand Up @@ -539,15 +533,7 @@ export function entryUpdateScript(contentType) {
async function updateEntry(entry, entryDetails) {
Object.assign(entry, { ...entryDetails });
await entry.update().catch(err => {
let errorMsg = 'Entry update failed for uid: ' + entry?.uid + ', title: ' + entry?.title + '.';
if(err?.errors?.title){
errorMsg += err?.errors?.title;
}else if(err?.errors?.entry){
errorMsg += err?.errors?.entry;
}else{
errorMsg += (err?.entry?.errorMessage || err?.errorMessage || err?.message) ?? 'Something went wrong!';
}
logger.error(errorMsg)
throw err
});
}

Expand Down Expand Up @@ -620,7 +606,7 @@ export function entryUpdateScript(contentType) {
.entry()
.create({ entry: entryDetails })
.catch(err => {
(err?.errorMessage || err?.message) ? err?.errorMessage || err?.message : 'Something went wrong!'
throw err;
})

if(createdEntry){
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-migration/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/cli-migration",
"version": "1.5.1",
"version": "1.5.2",
"author": "@contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down Expand Up @@ -66,4 +66,4 @@
"cm:migration": "O-MGRTN"
}
}
}
}
33 changes: 26 additions & 7 deletions packages/contentstack-migration/src/utils/error-helper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const { highlight } = require('cardinal');
const { keys } = Object;
const chalk = require('chalk');
const isEmpty = require('lodash/isEmpty')

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 @@ -19,41 +21,58 @@ const getLineWithContext = (lines, lineNumber, context) => {
};
};

function removeSpecialCharacter(str) {
return str.replace(/\u001b\[\d+m/g, '');
}

module.exports = (errors) => {
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');

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;
const { before, line, after } = getLineWithContext(lines, callsite.line, context);
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`;

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) {
return chalk`{red Line ${String(callsite.line)}:} {bold ${error.payload.apiError.message}}\n${formattedCode}`;
errorLogs[file].apiError = true;
errorLogs[file].errorCode = error.payload.apiError.errorCode;
errorLogs[file].errors = error.payload.apiError.errors;
}
if (error.message) {
return chalk`{red Line ${String(callsite.line)}:} {bold ${error.message}}\n${formattedCode}`;
if (error.message && !error.payload.apiError) {
errorLogs[file].apiError = false;
errorLogs[file].error = error.message;
}
return chalk`{red Line ${String(callsite.line)}:} {bold something went wrong here.}\n${formattedCode}`;
})
.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'));
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
}
// eslint-disable-next-line
Expand Down
28 changes: 28 additions & 0 deletions packages/contentstack-migration/src/utils/migration-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const winston = require('winston');
const path = require('path');
module.exports = class MigrationLogger {
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
constructor(filePath) {
this.filePath = path.join(filePath, 'migration-logs');
this.logger = winston.createLogger({
levels: { info: 1, error: 2 },
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()),
}),
],
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
});
}
log(level, message) {
if (level === 'info') {
this.logger.log('info', message);
}
this.logger.log('error', message);
}
};
2 changes: 1 addition & 1 deletion packages/contentstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@contentstack/cli-command": "~1.2.17",
"@contentstack/cli-config": "~1.6.3",
"@contentstack/cli-launch": "~1.0.17",
"@contentstack/cli-migration": "~1.5.0",
"@contentstack/cli-migration": "~1.5.2",
"@contentstack/cli-utilities": "~1.6.0",
"@contentstack/management": "~1.15.3",
"@oclif/core": "^2.9.3",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading