Handles error messages

This commit is contained in:
Pugazhendhi
2025-01-23 11:11:25 +05:30
committed by Vignesh Subbiah
parent 20d9a88bb4
commit 698ae6566d

View File

@@ -12,6 +12,7 @@ export class UnboundHandler implements ApiHandler {
} }
async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream { async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream {
try {
const response = await fetch(`${this.unboundBaseUrl}/chat/completions`, { const response = await fetch(`${this.unboundBaseUrl}/chat/completions`, {
method: "POST", method: "POST",
headers: { headers: {
@@ -24,12 +25,12 @@ export class UnboundHandler implements ApiHandler {
}), }),
}) })
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json() const data = await response.json()
if (!response.ok) {
throw new Error(data.error)
}
yield { yield {
type: "text", type: "text",
text: data.choices[0]?.message?.content || "", text: data.choices[0]?.message?.content || "",
@@ -39,6 +40,12 @@ export class UnboundHandler implements ApiHandler {
inputTokens: data.usage?.prompt_tokens || 0, inputTokens: data.usage?.prompt_tokens || 0,
outputTokens: data.usage?.completion_tokens || 0, outputTokens: data.usage?.completion_tokens || 0,
} }
} catch (error) {
if (error instanceof Error) {
throw new Error(`Unbound Gateway completion error: ${error.message}`)
}
throw error
}
} }
getModel(): { id: UnboundModelId; info: ModelInfo } { getModel(): { id: UnboundModelId; info: ModelInfo } {