From ec2230dd7913a024b7d38f2c58066ba12165cc49 Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Wed, 14 Jul 2021 11:11:31 +1000 Subject: [PATCH] fix(twilio-run): handles ngrok load error and actual ngrok errors (#305) Fixes #275. --- .../__tests__/config/start.withoutNgrok.test.ts | 16 ++++++++++------ packages/twilio-run/src/commands/start.ts | 8 +++++--- packages/twilio-run/src/config/start.ts | 11 +++++++++-- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/twilio-run/__tests__/config/start.withoutNgrok.test.ts b/packages/twilio-run/__tests__/config/start.withoutNgrok.test.ts index 60aac2de..be51b44e 100644 --- a/packages/twilio-run/__tests__/config/start.withoutNgrok.test.ts +++ b/packages/twilio-run/__tests__/config/start.withoutNgrok.test.ts @@ -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.' + ); } }); }); diff --git a/packages/twilio-run/src/commands/start.ts b/packages/twilio-run/src/commands/start.ts index b1d08c4b..3ace1c96 100644 --- a/packages/twilio-run/src/commands/start.ts +++ b/packages/twilio-run/src/commands/start.ts @@ -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); }); } diff --git a/packages/twilio-run/src/config/start.ts b/packages/twilio-run/src/config/start.ts index 56f537f3..773c55cd 100644 --- a/packages/twilio-run/src/config/start.ts +++ b/packages/twilio-run/src/config/start.ts @@ -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); }