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)
} 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"
)
}