fix: 增加错误输出

This commit is contained in:
ikechan8370 2023-09-19 11:17:43 +08:00
parent c36a0f9c9c
commit 7c5e9ea434

View file

@ -156,31 +156,37 @@ export class ClaudeAIClient {
if (streamDataRes.status === 307) { if (streamDataRes.status === 307) {
throw new Error('claude.ai目前不支持你所在的地区') throw new Error('claude.ai目前不支持你所在的地区')
} }
let streamData = streamDataRes.body if (streamDataRes.status === 200) {
// console.log(streamData) let streamData = streamDataRes.body
let responseText = '' // console.log(streamData)
let streams = streamData.split('\n\n') let responseText = ''
for (let s of streams) { let streams = streamData.split('\n\n')
let jsonStr = s.replace('data: ', '').trim() for (let s of streams) {
try { let jsonStr = s.replace('data: ', '').trim()
let jsonObj = JSON.parse(jsonStr) try {
if (jsonObj && jsonObj.completion) { let jsonObj = JSON.parse(jsonStr)
responseText += jsonObj.completion if (jsonObj && jsonObj.completion) {
} responseText += jsonObj.completion
if (this.debug) { }
console.log(jsonObj) if (this.debug) {
} console.log(jsonObj)
// console.log(responseText) }
} catch (err) { // console.log(responseText)
// ignore error } catch (err) {
if (this.debug) { // ignore error
console.log(jsonStr) if (this.debug) {
console.log(jsonStr)
}
} }
} }
} return {
return { text: responseText.trim(),
text: responseText.trim(), conversationId
conversationId }
} else if (streamDataRes.status === 408) {
throw new Error('claude.ai响应超时可能是回复文本太多请调高超时时间重试')
} else {
throw new Error('unknown error')
} }
} }
} }