mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-06-11 09:51:40 +00:00
fix(shadcn): avoid Error.cause typings for older TS lib targets
This commit is contained in:
@@ -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"
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user