fix: update how ky handle error (#34735)

This commit is contained in:
Stephen Zhou
2026-04-08 10:38:33 +00:00
committed by GitHub
parent 9948a51b14
commit 63bfba0bdb
7 changed files with 62 additions and 12 deletions
+21 -1
View File
@@ -38,6 +38,26 @@ export type ResponseError = {
status: number
}
const createResponseFromHTTPError = (error: HTTPError): Response => {
const headers = new Headers(error.response.headers)
headers.delete('content-length')
let body: BodyInit | null = null
if (typeof error.data === 'string')
body = error.data
else if (error.data !== undefined)
body = JSON.stringify(error.data)
if (body !== null && !headers.has('content-type'))
headers.set('content-type', ContentType.json)
return new Response(body, {
status: error.response.status,
statusText: error.response.statusText,
headers,
})
}
const afterResponseErrorCode = (otherOptions: IOtherOptions): AfterResponseHook => {
return async ({ response }) => {
if (!/^([23])\d{2}$/.test(String(response.status))) {
@@ -209,7 +229,7 @@ async function base<T>(url: string, options: FetchOptionType = {}, otherOptions:
}
catch (error) {
if (error instanceof HTTPError)
throw error.response.clone()
throw createResponseFromHTTPError(error)
throw error
}