From 01b72a39ba43a3c7fac421b9d6f092bd6fb604e6 Mon Sep 17 00:00:00 2001 From: shadcn Date: Wed, 10 Jun 2026 12:25:03 +0400 Subject: [PATCH] fix(shadcn): avoid Error.cause typings for older TS lib targets --- packages/shadcn/src/registry/proxy.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/packages/shadcn/src/registry/proxy.ts b/packages/shadcn/src/registry/proxy.ts index 33abd1dbbd..9a28037f8f 100644 --- a/packages/shadcn/src/registry/proxy.ts +++ b/packages/shadcn/src/registry/proxy.ts @@ -21,13 +21,21 @@ export async function fetchWithProxy(url: string | URL, init?: RequestInit) { } as RequestInit) } catch (error) { // Native fetch reports network failures as a generic "fetch failed" - // TypeError with the actual reason buried in `cause`. - if (error instanceof TypeError && error.cause) { - throw new Error( - `Request to ${url} failed, reason: ${getFailureReason(error.cause)}`, - { cause: error.cause } - ) + // TypeError with the actual reason buried in `cause`. The casts are + // needed because the configured TS lib predates Error.cause. + const cause = + error instanceof TypeError + ? (error as TypeError & { cause?: unknown }).cause + : undefined + + if (cause) { + const enriched = new Error( + `Request to ${url} failed, reason: ${getFailureReason(cause)}` + ) as Error & { cause?: unknown } + enriched.cause = cause + throw enriched } + throw error } } @@ -44,9 +52,7 @@ function getFailureReason(cause: unknown): string { if (cause instanceof Error) { return ( - cause.message || - (cause as NodeJS.ErrnoException).code || - "unknown error" + cause.message || (cause as NodeJS.ErrnoException).code || "unknown error" ) }