From d27cf6df7bd612642a1e8da5948333b00b70e8bd Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Mon, 29 Jul 2024 13:49:01 +0100 Subject: [PATCH] fix(create-astro): log fetch errors (#11567) --- .changeset/gorgeous-timers-tease.md | 5 +++++ packages/create-astro/src/actions/template.ts | 22 +++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 .changeset/gorgeous-timers-tease.md diff --git a/.changeset/gorgeous-timers-tease.md b/.changeset/gorgeous-timers-tease.md new file mode 100644 index 000000000000..33e8ec06a2f8 --- /dev/null +++ b/.changeset/gorgeous-timers-tease.md @@ -0,0 +1,5 @@ +--- +'create-astro': patch +--- + +Logs underlying error when a template cannot be downloaded diff --git a/packages/create-astro/src/actions/template.ts b/packages/create-astro/src/actions/template.ts index 3afd5f3cf47a..5169c63e30e0 100644 --- a/packages/create-astro/src/actions/template.ts +++ b/packages/create-astro/src/actions/template.ts @@ -83,7 +83,6 @@ export function getTemplateTarget(tmpl: string, ref = 'latest') { export default async function copyTemplate(tmpl: string, ctx: Context) { const templateTarget = getTemplateTarget(tmpl, ctx.ref); - // Copy if (!ctx.dryRun) { try { @@ -104,11 +103,26 @@ export default async function copyTemplate(tmpl: string, ctx: Context) { } } - if (err.message.includes('404')) { + if (err.message?.includes('404')) { throw new Error(`Template ${color.reset(tmpl)} ${color.dim('does not exist!')}`); - } else { - throw new Error(err.message); } + + if (err.message) { + error('error', err.message); + } + try { + // The underlying error is often buried deep in the `cause` property + // This is in a try/catch block in case of weirdnesses in accessing the `cause` property + if ('cause' in err) { + // This is probably included in err.message, but we can log it just in case it has extra info + error('error', err.cause); + if ('cause' in err.cause) { + // Hopefully the actual fetch error message + error('error', err.cause?.cause); + } + } + } catch {} + throw new Error(`Unable to download template ${color.reset(tmpl)}`); } // It's possible the repo exists (ex. `withastro/astro`),