Skip to content

Commit ec2230d

Browse files
authored
fix(twilio-run): handles ngrok load error and actual ngrok errors (#305)
Fixes #275.
1 parent b7a2035 commit ec2230d

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

packages/twilio-run/__tests__/config/start.withoutNgrok.test.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@ jest.mock('ngrok', () => {
66

77
describe('getUrl', () => {
88
test('calls ngrok if ngrok is defined', async () => {
9-
const config = ({
9+
const config = {
1010
ngrok: '',
11-
} as unknown) as StartCliFlags;
11+
} as unknown as StartCliFlags;
1212

1313
expect.assertions(1);
1414
try {
1515
await getUrl(config, 3000);
1616
} catch (error) {
17-
expect(error.message).toMatch("Cannot find module 'ngrok'");
17+
expect(error.message).toMatch(
18+
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
19+
);
1820
}
1921
});
2022

2123
test('calls ngrok with custom subdomain if passed', async () => {
22-
const config = ({
24+
const config = {
2325
ngrok: 'dom',
24-
} as unknown) as StartCliFlags;
26+
} as unknown as StartCliFlags;
2527

2628
expect.assertions(1);
2729
try {
2830
await getUrl(config, 3000);
2931
} catch (error) {
30-
expect(error.message).toMatch("Cannot find module 'ngrok'");
32+
expect(error.message).toMatch(
33+
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
34+
);
3135
}
3236
});
3337
});

packages/twilio-run/src/commands/start.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ export async function handler(
9090
resolve();
9191
} catch (error) {
9292
server.close(() => {
93-
logger.info(
94-
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
95-
);
93+
if (error.msg && error.details && error.details.err) {
94+
logger.error(`${error.msg}\n\n${error.details.err}`);
95+
} else {
96+
logger.error(error.message);
97+
}
9698
process.exit(1);
9799
});
98100
}

packages/twilio-run/src/config/start.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,15 @@ export async function getUrl(cli: StartCliFlags, port: string | number) {
7272
if (typeof cli.ngrok === 'string' && cli.ngrok.length > 0) {
7373
ngrokConfig.subdomain = cli.ngrok;
7474
}
75-
76-
url = await require('ngrok').connect(ngrokConfig);
75+
let ngrok;
76+
try {
77+
ngrok = require('ngrok');
78+
} catch (error) {
79+
throw new Error(
80+
'ngrok could not be started because the module is not installed. Please install optional dependencies and try again.'
81+
);
82+
}
83+
url = await ngrok.connect(ngrokConfig);
7784
debug('ngrok tunnel URL: %s', url);
7885
}
7986

0 commit comments

Comments
 (0)