Skip to content

Commit

Permalink
fix: add handling for error http status code for version check
Browse files Browse the repository at this point in the history
  • Loading branch information
kakha urigashvili authored and RonWang committed May 20, 2020
1 parent 3d8b23a commit 8fd9b54
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/commands/abstract-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ class AbstractCommand {
url: `${CONSTANTS.NPM_REGISTRY_URL_BASE}/${CONSTANTS.APPLICATION_NAME}/latest`,
method: CONSTANTS.HTTP_REQUEST.VERB.GET
}, 'GET_NPM_REGISTRY', doDebug, (err, response) => {
if (err) {
Messenger.getInstance().error(`Failed to get the latest version for ${CONSTANTS.APPLICATION_NAME} from NPM registry.\n${err}\n`);
if (err || response.statusCode > 300) {
const error = err || `Http Status Code: ${response.statusCode}.`;
Messenger.getInstance().error(`Failed to get the latest version for ${CONSTANTS.APPLICATION_NAME} from NPM registry.\n${error}\n`);
} else {
const BANNER_WITH_HASH = '##########################################################################';
const latestVersion = JSON.parse(response.body).version;
Expand Down
18 changes: 18 additions & 0 deletions test/unit/commands/abstract-command-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,24 @@ describe('Command test - AbstractCommand class', () => {
});
});

it('| http client request error status code , should warn it out and pass the process', (done) => {
// setup
httpClient.request.yields(undefined, { statusCode: 400 });
// call
AbstractCommand.prototype._remindsIfNewVersion(TEST_DO_DEBUG_FALSE, undefined, (err) => {
// verify
expect(httpClient.request.args[0][0].url).equal(
`${CONSTANTS.NPM_REGISTRY_URL_BASE}/${CONSTANTS.APPLICATION_NAME}/latest`
);
expect(httpClient.request.args[0][0].method).equal(CONSTANTS.HTTP_REQUEST.VERB.GET);
expect(errorStub.args[0][0]).equal(
`Failed to get the latest version for ${CONSTANTS.APPLICATION_NAME} from NPM registry.\nHttp Status Code: 400.\n`
);
expect(err).equal(undefined);
done();
});
});

it('| new major version released, should error out and pass the process', (done) => {
// setup
const latestVersion = `${currentMajor + 1}.0.0`;
Expand Down

0 comments on commit 8fd9b54

Please sign in to comment.