Skip to content

Commit

Permalink
fix(twilio-run): handles ngrok load error and actual ngrok errors (#305)
Browse files Browse the repository at this point in the history
Fixes #275.
  • Loading branch information
philnash authored Jul 14, 2021
1 parent b7a2035 commit ec2230d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
16 changes: 10 additions & 6 deletions packages/twilio-run/__tests__/config/start.withoutNgrok.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,32 @@ jest.mock('ngrok', () => {

describe('getUrl', () => {
test('calls ngrok if ngrok is defined', async () => {
const config = ({
const config = {
ngrok: '',
} as unknown) as StartCliFlags;
} as unknown as StartCliFlags;

expect.assertions(1);
try {
await getUrl(config, 3000);
} catch (error) {
expect(error.message).toMatch("Cannot find module 'ngrok'");
expect(error.message).toMatch(
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
);
}
});

test('calls ngrok with custom subdomain if passed', async () => {
const config = ({
const config = {
ngrok: 'dom',
} as unknown) as StartCliFlags;
} as unknown as StartCliFlags;

expect.assertions(1);
try {
await getUrl(config, 3000);
} catch (error) {
expect(error.message).toMatch("Cannot find module 'ngrok'");
expect(error.message).toMatch(
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
);
}
});
});
8 changes: 5 additions & 3 deletions packages/twilio-run/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ export async function handler(
resolve();
} catch (error) {
server.close(() => {
logger.info(
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
);
if (error.msg && error.details && error.details.err) {
logger.error(`${error.msg}\n\n${error.details.err}`);
} else {
logger.error(error.message);
}
process.exit(1);
});
}
Expand Down
11 changes: 9 additions & 2 deletions packages/twilio-run/src/config/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ export async function getUrl(cli: StartCliFlags, port: string | number) {
if (typeof cli.ngrok === 'string' && cli.ngrok.length > 0) {
ngrokConfig.subdomain = cli.ngrok;
}

url = await require('ngrok').connect(ngrokConfig);
let ngrok;
try {
ngrok = require('ngrok');
} catch (error) {
throw new Error(
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
);
}
url = await ngrok.connect(ngrokConfig);
debug('ngrok tunnel URL: %s', url);
}

Expand Down

0 comments on commit ec2230d

Please sign in to comment.