Skip to content

Commit 037d3f7

Browse files
author
timmydoza
authored
Merge pull request #28 from twilio-labs/add-api-key-error
Add api key error
2 parents 0a71af0 + 4028c8e commit 037d3f7

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.1.5
4+
5+
### Maintenance
6+
7+
* Updated `twilio rtc:apps:video:deploy` command so that it checks for the existence of a necessary Twilio API Key before deploying the video token server.
8+
39
## 0.1.4
410

511
### Maintenance

src/helpers.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { APP_NAME, EXPIRY_PERIOD } = require('./constants');
22
const { cli } = require('cli-ux');
3+
const { CLIError } = require('@oclif/errors');
34
const fs = require('fs');
45
const { getListOfFunctionsAndAssets } = require('@twilio-labs/serverless-api/dist/utils/fs');
56
const moment = require('moment');
@@ -21,18 +22,18 @@ function verifyAppDirectory(dirpath) {
2122
const hasIndexHTML = [...dir].includes('index.html');
2223

2324
if (!hasIndexHTML) {
24-
throw new Error(
25+
throw new CLIError(
2526
'The provided app-directory does not appear to be a valid app. There is no index.html found in the app-directory.'
2627
);
2728
}
2829
} catch (err) {
2930
switch (err.code) {
3031
case 'ENOENT':
31-
throw new Error('The provided app-directory does not exist.');
32+
throw new CLIError('The provided app-directory does not exist.');
3233
case 'ENOTDIR':
33-
throw new Error('The provided app-directory is not a directory.');
34+
throw new CLIError('The provided app-directory is not a directory.');
3435
default:
35-
throw new Error(err.message);
36+
throw new CLIError(err.message);
3637
}
3738
}
3839
}
@@ -112,6 +113,25 @@ async function displayAppInfo() {
112113
async function deploy() {
113114
const assets = this.flags['app-directory'] ? await getAssets(this.flags['app-directory']) : [];
114115

116+
if (this.twilioClient.username === this.twilioClient.accountSid) {
117+
// When twilioClient.username equals twilioClient.accountSid, it means that the user
118+
// authenticated with the Twilio CLI by providing their Account SID and Auth Token
119+
// as environment variables. When this happens, the CLI does not create the required API
120+
// key that is needed by the token server.
121+
122+
throw new CLIError(`No API Key found.
123+
124+
Please login to the Twilio CLI to create an API key:
125+
126+
twilio login
127+
128+
Alternatively, the Twilio CLI can use credentials stored in these environment variables:
129+
130+
TWILIO_ACCOUNT_SID = your Account SID from twil.io/console
131+
TWILIO_API_KEY = an API Key created at twil.io/get-api-key
132+
TWILIO_API_SECRET = the secret for the API Key`);
133+
}
134+
115135
const serverlessClient = new TwilioServerlessApiClient({
116136
accountSid: this.twilioClient.username,
117137
authToken: this.twilioClient.password,

test/helpers/helpers.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,29 @@ describe('the deploy function', () => {
265265
expect(mockDeployProject.mock.calls[0][0].serviceSid).toBe(undefined);
266266
expect(mockDeployProject.mock.calls[0][0].serviceName).toBe(APP_NAME);
267267
});
268+
269+
it('should display an error when the API key is not provided', () => {
270+
return expect(
271+
deploy.call({
272+
twilioClient: {
273+
username: 'testAccountSid',
274+
password: 'testAuthToken',
275+
accountSid: 'testAccountSid',
276+
},
277+
flags: {},
278+
})
279+
).rejects.toMatchInlineSnapshot(`
280+
[Error: No API Key found.
281+
282+
Please login to the Twilio CLI to create an API key:
283+
284+
twilio login
285+
286+
Alternatively, the Twilio CLI can use credentials stored in these environment variables:
287+
288+
TWILIO_ACCOUNT_SID = your Account SID from twil.io/console
289+
TWILIO_API_KEY = an API Key created at twil.io/get-api-key
290+
TWILIO_API_SECRET = the secret for the API Key]
291+
`);
292+
});
268293
});

0 commit comments

Comments
 (0)