fix(shadcn): avoid Error.cause typings for older TS lib targets

This commit is contained in:
shadcn
2026-06-10 12:25:03 +04:00
parent 2be26ddbb7
commit 01b72a39ba

View File

@@ -21,13 +21,21 @@ export async function fetchWithProxy(url: string | URL, init?: RequestInit) {
} as RequestInit) } as RequestInit)
} catch (error) { } catch (error) {
// Native fetch reports network failures as a generic "fetch failed" // Native fetch reports network failures as a generic "fetch failed"
// TypeError with the actual reason buried in `cause`. // TypeError with the actual reason buried in `cause`. The casts are
if (error instanceof TypeError && error.cause) { // needed because the configured TS lib predates Error.cause.
throw new Error( const cause =
`Request to ${url} failed, reason: ${getFailureReason(error.cause)}`, error instanceof TypeError
{ cause: error.cause } ? (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 throw error
} }
} }
@@ -44,9 +52,7 @@ function getFailureReason(cause: unknown): string {
if (cause instanceof Error) { if (cause instanceof Error) {
return ( return (
cause.message || cause.message || (cause as NodeJS.ErrnoException).code || "unknown error"
(cause as NodeJS.ErrnoException).code ||
"unknown error"
) )
} }