From 63fc1bec5d411ce3264afcc299f4f71e386e0b8b Mon Sep 17 00:00:00 2001 From: Phil Nash Date: Tue, 21 Apr 2020 11:39:54 +1000 Subject: [PATCH] fix(templating): catch errors in command, not before. create-twilio-function uses downloadTemplate from src/templating/actions and that would previously throw an error for a missing/nonexistant template, but it changed to catch it. Downstream functions shouldn't catch and log errors because the function consumers won't know if something went wrong. --- src/commands/new.ts | 8 ++++++-- src/templating/actions.ts | 29 +++++++++++++---------------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/commands/new.ts b/src/commands/new.ts index 14c35d33..1655a994 100644 --- a/src/commands/new.ts +++ b/src/commands/new.ts @@ -5,7 +5,7 @@ import { Merge } from 'type-fest'; import { Arguments, Argv } from 'yargs'; import checkProjectStructure from '../checks/project-structure'; import { downloadTemplate, fetchListOfTemplates } from '../templating/actions'; -import { setLogLevelByName } from '../utils/logger'; +import { setLogLevelByName, logger } from '../utils/logger'; import { baseCliOptions, BaseFlags, ExternalCliOptions } from './shared'; import { CliInfo } from './types'; import { getFullCommand } from './utils'; @@ -112,7 +112,11 @@ export async function handler( const sanitizedNamespace = flags.namespace.replace(/\.js$/, ''); - downloadTemplate(flags.template, sanitizedNamespace, targetDirectory); + try { + await downloadTemplate(flags.template, sanitizedNamespace, targetDirectory); + } catch (error) { + logger.error(error.message, error.name); + } } export const cliInfo: CliInfo = { diff --git a/src/templating/actions.ts b/src/templating/actions.ts index ef026755..4cdd03af 100644 --- a/src/templating/actions.ts +++ b/src/templating/actions.ts @@ -9,22 +9,19 @@ export async function downloadTemplate( namespace: string, targetDirectory: string ): Promise { - try { - const files = await getTemplateFiles(templateName); - await writeFiles(files, targetDirectory, namespace, templateName); - logger.info( - chalk`{green SUCCESS} Downloaded new template into the "${namespace}" subdirectories.` - ); - logger.info( - `Check ${path.join( - 'readmes', - namespace, - `${templateName}.md` - )} for template instructions.` - ); - } catch (err) { - logger.error(err.message, err.name); - } + const files = await getTemplateFiles(templateName); + + await writeFiles(files, targetDirectory, namespace, templateName); + logger.info( + chalk`{green SUCCESS} Downloaded new template into the "${namespace}" subdirectories.` + ); + logger.info( + `Check ${path.join( + 'readmes', + namespace, + `${templateName}.md` + )} for template instructions.` + ); } export { fetchListOfTemplates } from './data';