Skip to content

Commit

Permalink
Add error handling for response validation in providers
Browse files Browse the repository at this point in the history
  • Loading branch information
ae86jack committed Nov 4, 2024
1 parent 401a924 commit d1ed8a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/providers/claude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ const sendRequestFunc = (settings: ClaudeOptions): SendRequest =>
agent: proxyUrl ? new HttpsProxyAgent(proxyUrl) : undefined
})

if (!response || !response.body) {
throw new Error('No response')
}
if (!response.ok) {
console.error('response', response)
throw new Error(`Unexpected response status: ${response.status} ${response.statusText}`)
}

// @ts-ignore
const stream = Stream.fromSSEResponse<PartialEvent>(response, new AbortController())

Expand Down
4 changes: 4 additions & 0 deletions src/providers/doubao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const sendRequestFunc = (settings: BaseOptions): SendRequest =>
if (!response || !response.body) {
throw new Error('No response')
}
if (!response.ok) {
console.error('response', response)
throw new Error(`Unexpected response status: ${response.status} ${response.statusText}`)
}
const decoder = new TextDecoder('utf-8')
for await (const chunk of response.body) {
const lines = decoder.decode(Buffer.from(chunk))
Expand Down
5 changes: 5 additions & 0 deletions src/providers/qianFan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ const sendRequestFunc = (settings: QianFanOptions): SendRequest =>
if (!response || !response.body) {
throw new Error('No response')
}
if (!response.ok) {
console.error('response', response)
throw new Error(`Unexpected response status: ${response.status} ${response.statusText}`)
}

const buffer: string[] = []
const decoder = new TextDecoder('utf-8')
for await (const chunk of response.body) {
Expand Down

0 comments on commit d1ed8a3

Please sign in to comment.