mirror of
https://github.com/langgenius/webapp-conversation.git
synced 2025-12-24 08:36:39 +08:00
fix: res is not json
This commit is contained in:
@ -62,7 +62,8 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
||||
const lines = buffer.split('\n')
|
||||
try {
|
||||
lines.forEach((message) => {
|
||||
if (!message) return
|
||||
if (!message)
|
||||
return
|
||||
bufferObj = JSON.parse(message) // remove data: and parse as json
|
||||
onData(unicodeToChar(bufferObj.answer), isFirstMessage, {
|
||||
conversationId: bufferObj.conversation_id,
|
||||
@ -71,11 +72,12 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
||||
isFirstMessage = false
|
||||
})
|
||||
buffer = lines[lines.length - 1]
|
||||
} catch (e) {
|
||||
}
|
||||
catch (e) {
|
||||
onData('', false, {
|
||||
conversationId: undefined,
|
||||
messageId: '',
|
||||
errorMessage: e + ''
|
||||
errorMessage: `${e}`,
|
||||
})
|
||||
return
|
||||
}
|
||||
@ -89,7 +91,7 @@ const handleStream = (response: any, onData: IOnData, onCompleted?: IOnCompleted
|
||||
const baseFetch = (url: string, fetchOptions: any, { needAllResponseContent }: IOtherOptions) => {
|
||||
const options = Object.assign({}, baseOptions, fetchOptions)
|
||||
|
||||
let urlPrefix = API_PREFIX
|
||||
const urlPrefix = API_PREFIX
|
||||
|
||||
let urlWithPrefix = `${urlPrefix}${url.startsWith('/') ? url : `/${url}`}`
|
||||
|
||||
@ -125,27 +127,32 @@ const baseFetch = (url: string, fetchOptions: any, { needAllResponseContent }: I
|
||||
const resClone = res.clone()
|
||||
// Error handler
|
||||
if (!/^(2|3)\d{2}$/.test(res.status)) {
|
||||
const bodyJson = res.json()
|
||||
switch (res.status) {
|
||||
case 401: {
|
||||
Toast.notify({ type: 'error', message: 'Invalid token' })
|
||||
return
|
||||
|
||||
}
|
||||
default:
|
||||
// eslint-disable-next-line no-new
|
||||
new Promise(() => {
|
||||
bodyJson.then((data: any) => {
|
||||
Toast.notify({ type: 'error', message: data.message })
|
||||
try {
|
||||
const bodyJson = res.json()
|
||||
switch (res.status) {
|
||||
case 401: {
|
||||
Toast.notify({ type: 'error', message: 'Invalid token' })
|
||||
return
|
||||
}
|
||||
default:
|
||||
// eslint-disable-next-line no-new
|
||||
new Promise(() => {
|
||||
bodyJson.then((data: any) => {
|
||||
Toast.notify({ type: 'error', message: data.message })
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
Toast.notify({ type: 'error', message: `${e}` })
|
||||
}
|
||||
|
||||
return Promise.reject(resClone)
|
||||
}
|
||||
|
||||
// handle delete api. Delete api not return content.
|
||||
if (res.status === 204) {
|
||||
resolve({ result: "success" })
|
||||
resolve({ result: 'success' })
|
||||
return
|
||||
}
|
||||
|
||||
@ -162,8 +169,7 @@ const baseFetch = (url: string, fetchOptions: any, { needAllResponseContent }: I
|
||||
])
|
||||
}
|
||||
|
||||
export const ssePost = (url: string, fetchOptions: any, {
|
||||
onData, onCompleted, onError }: IOtherOptions) => {
|
||||
export const ssePost = (url: string, fetchOptions: any, { onData, onCompleted, onError }: IOtherOptions) => {
|
||||
const options = Object.assign({}, baseOptions, {
|
||||
method: 'POST',
|
||||
}, fetchOptions)
|
||||
|
||||
Reference in New Issue
Block a user