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 6 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
8 changes: 4 additions & 4 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions packages/contentstack-branches/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-cm-branches",
"description": "Contentstack CLI plugin to do branches operations",
"version": "1.0.23",
"version": "1.0.24",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"dependencies": {
Expand Down Expand Up @@ -95,4 +95,4 @@
}
},
"repository": "https://github.com/contentstack/cli"
}
}
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
44 changes: 10 additions & 34 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 @@ -520,17 +514,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)
});
await entry.update()
}

async function updateReferences(entryDetails, baseEntry, references) {
Expand Down Expand Up @@ -558,29 +542,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 => {
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)
});
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 @@ -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 @@ -539,17 +533,7 @@ export function entryCreateUpdateScript(contentType) {
async function updateEntry(entry, entryDetails) {
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)
});
await entry.update();
}
}

Expand Down Expand Up @@ -622,9 +606,6 @@ export function entryCreateUpdateScript(contentType) {
.contentType('${contentType}')
.entry()
.create({ entry: entryDetails })
.catch(err => {
(err?.errorMessage || err?.message) ? err?.errorMessage || err?.message : 'Something went wrong!'
})

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 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 @@ -538,17 +532,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)
});
await entry.update();
}

async function updateReferences(entryDetails, baseEntry, references) {
Expand Down Expand Up @@ -619,9 +603,6 @@ export function entryUpdateScript(contentType) {
.contentType('${contentType}')
.entry()
.create({ entry: entryDetails })
.catch(err => {
(err?.errorMessage || err?.message) ? err?.errorMessage || err?.message : 'Something went wrong!'
})

if(createdEntry){
if (flag.references) {
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"
}
}
}
}
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`)) {
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
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 });
// }
cs-raj marked this conversation as resolved.
Show resolved Hide resolved
// throw new Error(err);
};
Loading
Loading