Skip to content

Commit

Permalink
refactor: added error handling to vonage apps list
Browse files Browse the repository at this point in the history
  • Loading branch information
manchuck committed Oct 30, 2024
1 parent b3c1f1e commit edb6695
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
19 changes: 17 additions & 2 deletions __tests__/commands/apps/apps.list.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
process.env.FORCE_COLOR = 0;
const yaml = require('yaml');
const yargs = require('yargs');
const {
getTestApp,
addVerifyCapabilities,
Expand All @@ -19,6 +20,7 @@ const { spinner } = require('../../../src/ux/spinner');
const { Vonage } = require('@vonage/server-sdk');
const { Client } = require('@vonage/server-client');

jest.mock('yargs');
jest.mock('@vonage/server-sdk');
jest.mock('../../../src/ux/spinner');

Expand All @@ -31,7 +33,6 @@ describe('Command: vonage apps', () => {
});

test('Will list applications when there are none', async () => {

const sdk = Vonage();

Vonage._mockListAllApplications.mockImplementation(async function* () {
Expand Down Expand Up @@ -251,7 +252,7 @@ describe('Command: vonage apps', () => {
expect(console.log).toHaveBeenCalledWith(yaml.stringify([Client.transformers.snakeCaseObjectKeys(app, true)], null, 2));
});

test('Should error when capability is not valid', async () => {
test('Will error when capability is not valid', async () => {
expect(() => coerceCapability('invalid'))
.toThrow('Invalid capability. Only: messages, network_apis, rtc, vbc, verify, video, voice are allowed');

Expand All @@ -261,4 +262,18 @@ describe('Command: vonage apps', () => {
expect(() => coerceCapability('invalid+foo'))
.toThrow('Invalid capability. Only: messages, network_apis, rtc, vbc, verify, video, voice are allowed');
});

test('Will exit 99 when API calls fails', async () => {
const sdk = Vonage();

Vonage._mockListAllApplications.mockImplementation(async function* () {
yield *[];
throw new Error('API Error');
});

await handler({ SDK: sdk });

expect(console.table).not.toHaveBeenCalled();
expect(yargs.exit).toHaveBeenCalledWith(99);
});
});
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@vonage/cli",
"version": "3.0.0",
"version": "3.0.0.beta.1",
"description": "Vonage CLI",
"license": "Apache 2.0",
"contributors": [
Expand Down Expand Up @@ -73,5 +73,8 @@
"jest": "29.7.0",
"lint-staged": "15.2.10",
"winston-transport": "4.8.0"
},
"engines": {
"node": ">=18.0.0"
}
}
4 changes: 2 additions & 2 deletions src/commands/apps/list.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const yaml = require('yaml');
const { sdkError } = require('../../utils/sdkError');
const { spinner } = require('../../ux/spinner');
const snakecase = require('snakecase');
const { Client } = require('@vonage/server-client');
Expand Down Expand Up @@ -107,8 +108,7 @@ exports.handler = async (argv) => {
}
} catch (error) {
fail();
console.error('❌ Loading applications... Failed');
console.error(error);
sdkError(error);
return;
}

Expand Down

0 comments on commit edb6695

Please sign in to comment.