Skip to content

Commit

Permalink
log push errors to the console
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-zoharmuzafi committed Aug 8, 2018
1 parent e5c3e96 commit a9aca72
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
6 changes: 4 additions & 2 deletions bin/stencil-push
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const pkg = require('../package.json');
const Program = require('commander');
const stencilPush = require('../lib/stencil-push');
const versionCheck = require('../lib/version-check');
const themeApiClient = require('../lib/theme-api-client');

Program
.version(pkg.version)
Expand All @@ -28,8 +29,9 @@ stencilPush(Object.assign({}, options, {
saveBundleName: Program.save
}), (err, result) => {
if (err) {
console.log('not ok'.red + ` -- ${err}`);
console.log('Please try again. If this error persists, please visit https://github.com/bigcommerce/stencil-cli/issues and submit an issue.');
console.log("\n\n" + 'not ok'.red + ` -- ${err} see details below:`);
themeApiClient.printErrorMessages(err.messages)
console.log('If this error persists, please visit https://github.com/bigcommerce/stencil-cli/issues and submit an issue.');
} else {
console.log('ok'.green + ` -- ${result}`);
}
Expand Down
21 changes: 21 additions & 0 deletions lib/theme-api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const themeApiClient = {
activateThemeByVariationId,
deleteThemeById,
getJob,
printErrorMessages,
getVariationsByThemeId,
getThemes,
postTheme,
Expand Down Expand Up @@ -76,6 +77,7 @@ function getJob(options, callback) {
if (res.statusCode !== 200 || payload.data.status === 'FAILED') {
error = new Error('Job Failed');
error.name = "JobCompletionStatusCheckError";
error.messages = payload.data.errors
return callback(error);
}

Expand All @@ -91,6 +93,25 @@ function getJob(options, callback) {
});
}

function printErrorMessages(errors_array) {
if (!Array.isArray(errors_array)) {
console.log("unknown error".red)
return false
}

for (var i = 0; i < errors_array.length; i++) {
try{
console.log(errors_array[i].message.red + '\n');
}
catch(err) {
continue;
}
}

console.log('Please visit the troubleshooting page https://stencil.bigcommerce.com/docs/uploading-a-custom-theme');
return true
}

function getThemes(options, callback) {
Wreck.get(`${options.apiHost}/stores/${options.storeHash}/v3/themes`, {
headers: {
Expand Down
54 changes: 54 additions & 0 deletions lib/theme-api-client.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

const Code = require('code');
const Lab = require('lab');
const lab = exports.lab = Lab.script();
const describe = lab.describe;
const expect = Code.expect;
const themeApiClient = require('./theme-api-client');
const it = lab.it;
const sinon = require('sinon');

describe('theme-api-client', () => {
describe('printErrorMessages()', () => {
var consoleLogStub;

lab.before(done => {
consoleLogStub = sinon.stub(console, 'log');
done();
});

lab.after((done) => {
console.log.restore();
done();
});

it('should log unknown error and return if input is not an array', done => {
expect(themeApiClient.printErrorMessages("string")).to.be.equal(false);
expect(consoleLogStub.calledOnce).to.be.equal(true);
consoleLogStub.resetHistory();
expect(themeApiClient.printErrorMessages({ "key": "value" })).to.be.equal(false);
expect(consoleLogStub.calledOnce).to.be.equal(true);
consoleLogStub.resetHistory();
expect(themeApiClient.printErrorMessages(null)).to.be.equal(false);
expect(consoleLogStub.calledOnce).to.be.equal(true);
done();
});

it('should log error message for each error in the array', done => {
consoleLogStub.resetHistory();
var arrayInput = [{ "message": "first_error" }, { "message": "2nd_error" }];
expect(themeApiClient.printErrorMessages(arrayInput)).to.be.equal(true);
expect(consoleLogStub.calledThrice).to.be.equal(true);
done();
});

it('should skip non object elements in the input array', done => {
consoleLogStub.resetHistory();
var arrayInput = [{ "message": "first_error" }, "string", { "message": "2nd_error" }];
expect(themeApiClient.printErrorMessages(arrayInput)).to.be.equal(true);
expect(consoleLogStub.calledThrice).to.be.equal(true);
done();
});
});
});

0 comments on commit a9aca72

Please sign in to comment.