Skip to content

Commit dd6ead4

Browse files
authored
Handle connection error on fallback (#122)
1 parent 8fea6f6 commit dd6ead4

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

gateway/src/gateway.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function gateway(
6161
const otel = new OtelTrace(request, apiKeyInfo.otelSettings, options)
6262

6363
// The AI did this, but I actually find it nice.
64-
let result!: Awaited<ReturnType<InstanceType<ReturnType<typeof getProvider>>['dispatch']>>
64+
let result: Awaited<ReturnType<InstanceType<ReturnType<typeof getProvider>>['dispatch']>> | null = null
6565

6666
for (const providerProxy of providerProxies) {
6767
const ProxyCls = getProvider(providerProxy.providerId)
@@ -79,7 +79,15 @@ export async function gateway(
7979
otelSpan,
8080
})
8181

82-
result = await proxy.dispatch()
82+
try {
83+
result = await proxy.dispatch()
84+
} catch (error) {
85+
logfire.reportError('Connection error', error as Error, {
86+
providerId: providerProxy.providerId,
87+
routingGroup: providerProxy.routingGroup,
88+
})
89+
continue
90+
}
8391

8492
// Those responses are already closing the `otelSpan`.
8593
if (!('responseStream' in result) && !('response' in result) && !('unexpectedStatus' in result)) {
@@ -101,6 +109,10 @@ export async function gateway(
101109
break
102110
}
103111

112+
if (!result) {
113+
return textResponse(500, 'Internal Server Error')
114+
}
115+
104116
let response: Response
105117
if ('response' in result) {
106118
response = result.response

0 commit comments

Comments
 (0)