From 698ae6566d998112e00927b1c3c8ceacd800e705 Mon Sep 17 00:00:00 2001 From: Pugazhendhi Date: Thu, 23 Jan 2025 11:11:25 +0530 Subject: [PATCH] Handles error messages --- src/api/providers/unbound.ts | 53 ++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/src/api/providers/unbound.ts b/src/api/providers/unbound.ts index 342e031..5e39798 100644 --- a/src/api/providers/unbound.ts +++ b/src/api/providers/unbound.ts @@ -12,32 +12,39 @@ export class UnboundHandler implements ApiHandler { } async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream { - const response = await fetch(`${this.unboundBaseUrl}/chat/completions`, { - method: "POST", - headers: { - Authorization: `Bearer ${this.options.unboundApiKey}`, - "Content-Type": "application/json", - }, - body: JSON.stringify({ - model: this.getModel().id, - messages: [{ role: "system", content: systemPrompt }, ...messages], - }), - }) + try { + const response = await fetch(`${this.unboundBaseUrl}/chat/completions`, { + method: "POST", + headers: { + Authorization: `Bearer ${this.options.unboundApiKey}`, + "Content-Type": "application/json", + }, + body: JSON.stringify({ + model: this.getModel().id, + messages: [{ role: "system", content: systemPrompt }, ...messages], + }), + }) - 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 { - type: "text", - text: data.choices[0]?.message?.content || "", - } - yield { - type: "usage", - inputTokens: data.usage?.prompt_tokens || 0, - outputTokens: data.usage?.completion_tokens || 0, + yield { + type: "text", + text: data.choices[0]?.message?.content || "", + } + yield { + type: "usage", + inputTokens: data.usage?.prompt_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 } }