Skip to content

Commit

Permalink
refactor: move printErrorMessages() from theme-api-client.js to cliCo…
Browse files Browse the repository at this point in the history
…mmon.js
  • Loading branch information
MaxGenash committed Sep 21, 2020
1 parent 6dcdd9b commit d2c259b
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 111 deletions.
8 changes: 2 additions & 6 deletions bin/stencil-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { promisify } = require("util");
const { API_HOST, PACKAGE_INFO, DOT_STENCIL_FILE_PATH } = require('../constants');
const stencilDownload = require('../lib/stencil-download');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');
const { printCliResultError } = require('../lib/cliCommon');

program
.version(PACKAGE_INFO.version)
Expand Down Expand Up @@ -52,11 +52,7 @@ async function run (opts) {
try {
await promisify(stencilDownload)(opts);
} catch (err) {
console.log('\n\n' + 'not ok'.red + ` -- ` + err);
if (err.messages) {
themeApiClient.printErrorMessages(err.messages);
}
console.log('If this error persists, please visit https:/bigcommerce/stencil-cli/issues and submit an issue.');
printCliResultError(err);
return;
}

Expand Down
12 changes: 4 additions & 8 deletions bin/stencil-pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { DOT_STENCIL_FILE_PATH, PACKAGE_INFO, API_HOST } = require('../constants'
const program = require('../lib/commander');
const stencilPull = require('../lib/stencil-pull');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');
const { printCliResultError} = require('../lib/cliCommon');

program
.version(PACKAGE_INFO.version)
Expand All @@ -27,12 +27,8 @@ const options = {

stencilPull(options, (err, result) => {
if (err) {
console.log('\n\n' + 'not ok'.red + ` -- ` + err);
if (err.messages) {
themeApiClient.printErrorMessages(err.messages);
}
console.log('If this error persists, please visit https:/bigcommerce/stencil-cli/issues and submit an issue.');
} else {
console.log('ok'.green + ` -- Pulled active theme config to ${result.saveConfigName}`);
printCliResultError(err);
return;
}
console.log('ok'.green + ` -- Pulled active theme config to ${result.saveConfigName}`);
});
12 changes: 4 additions & 8 deletions bin/stencil-push.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { DOT_STENCIL_FILE_PATH, PACKAGE_INFO, API_HOST } = require('../constants'
const program = require('../lib/commander');
const stencilPush = require('../lib/stencil-push');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');
const { printCliResultError } = require('../lib/cliCommon');

program
.version(PACKAGE_INFO.version)
Expand All @@ -31,12 +31,8 @@ const options = {
};
stencilPush(options, (err, result) => {
if (err) {
console.log('\n\n' + 'not ok'.red + ` -- ` + err);
if (err.messages) {
themeApiClient.printErrorMessages(err.messages);
}
console.log('If this error persists, please visit https:/bigcommerce/stencil-cli/issues and submit an issue.');
} else {
console.log('ok'.green + ` -- ${result}`);
printCliResultError(err);
return;
}
console.log('ok'.green + ` -- ${result}`);
});
30 changes: 30 additions & 0 deletions lib/cliCommon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const messages = {
visitTroubleshootingPage: 'Please visit the troubleshooting page https://developer.bigcommerce.com/stencil-docs/deploying-a-theme/troubleshooting-theme-uploads.',
submitGithubIssue: 'If this error persists, please visit https:/bigcommerce/stencil-cli/issues and submit an issue.',
};

/**
* @param {Error} error
* @param {Array<{message: string}>} [error.messages]
* @returns {void}
*/
function printCliResultError (error) {
console.log('\n\n' + 'not ok'.red + ` -- ` + (error || 'Unknown error') + '\n');

if (error && Array.isArray(error.messages)) {
for (let item of error.messages) {
if (item && item.message) {
console.log(item.message.red + '\n');
}
}
}

console.log(messages.visitTroubleshootingPage);

console.log(messages.submitGithubIssue);
}

module.exports = {
printCliResultError,
messages,
};
81 changes: 81 additions & 0 deletions lib/cliCommon.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require('colors');

const { printCliResultError, messages } = require('./cliCommon');

describe('cliCommon', () => {
describe('printCliResultError', () => {
let consoleLogStub;

beforeAll(() => {
consoleLogStub = jest.spyOn(console, 'log').mockImplementation(jest.fn());
});

afterEach(() => {
jest.clearAllMocks();
});

afterAll(() => {
jest.restoreAllMocks();
});

it('should log "Unknown error" and general recommendations if input is empty', () => {
printCliResultError(null);

expect(consoleLogStub).toHaveBeenCalledTimes(3);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching('Unknown error'));
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue);
});

it('should log the passed error and general recommendations if input is a plain Error object with no extra messages', () => {
const err = new Error('test error');

printCliResultError(err);

expect(consoleLogStub).toHaveBeenCalledTimes(3);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(err.toString()));
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue);
});

it('should log the passed message and general recommendations if input is a string', () => {
const errStr = 'test error message';

printCliResultError(errStr);

expect(consoleLogStub).toHaveBeenCalledTimes(3);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(errStr));
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue);
});


it('should log the error, each field in error.messages and general recommendations if input is an object with error.messages field', () => {
const err = new Error('test error');
err.messages = [{'message': 'first_error'}, {'message': '2nd_error'}];

printCliResultError(err);

expect(consoleLogStub).toHaveBeenCalledTimes(5);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(err.toString()));
expect(consoleLogStub).toHaveBeenCalledWith(err.messages[0].message.red + '\n');
expect(consoleLogStub).toHaveBeenCalledWith(err.messages[1].message.red + '\n');
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue);
});

it('should skip non object elements in the error.message array', () => {
const err = new Error('test error');
err.messages = [{'message': 'first_error'}, 'string', {'message': '2nd_error'}, undefined, null, 228, true];

printCliResultError(err);

expect(consoleLogStub).toHaveBeenCalledTimes(5);
expect(consoleLogStub).toHaveBeenCalledWith(expect.stringMatching(err.toString()));
expect(consoleLogStub).toHaveBeenCalledWith('first_error'.red + '\n');
expect(consoleLogStub).toHaveBeenCalledWith('2nd_error'.red + '\n');
expect(consoleLogStub).toHaveBeenCalledWith(messages.visitTroubleshootingPage);
expect(consoleLogStub).toHaveBeenCalledWith(messages.submitGithubIssue);
});
});
});
20 changes: 0 additions & 20 deletions lib/theme-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const themeApiClient = {
activateThemeByVariationId,
deleteThemeById,
getJob,
printErrorMessages,
getVariationsByThemeId,
getThemes,
postTheme,
Expand Down Expand Up @@ -128,25 +127,6 @@ async function getJob({ accessToken, apiHost, storeHash, jobId, resultFilter })
return resultFilter ? resultFilter(payload.data.result) : payload.data.result;
}

/**
* @param {object[]} errors
* @returns {void}
*/
function printErrorMessages(errors) {
if (!Array.isArray(errors)) {
console.log("unknown error".red);
return;
}

for (let error of errors) {
if (error && error.message) {
console.log(error.message.red + '\n');
}
}

console.log('Please visit the troubleshooting page https://developer.bigcommerce.com/stencil-docs/deploying-a-theme/troubleshooting-theme-uploads');
}

/**
* @param {object} options
* @param {string} options.accessToken
Expand Down
69 changes: 0 additions & 69 deletions lib/theme-api-client.spec.js

This file was deleted.

0 comments on commit d2c259b

Please sign in to comment.