mirror of
https://github.com/langgenius/dify.git
synced 2026-09-24 23:22:26 +08:00
fix: update how ky handle error (#34735)
This commit is contained in:
+21
-1
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user