From 3ca0677b2628338422e57aa42a2a3b9286c8a335 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 16 Aug 2026 14:43:30 +0200 Subject: [PATCH] Dont fail the gateway connection if unable to connect to an offload endpoint --- src/gateway/util/Utils.ts | 65 +++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/src/gateway/util/Utils.ts b/src/gateway/util/Utils.ts index adc7bbeca..cc710e572 100644 --- a/src/gateway/util/Utils.ts +++ b/src/gateway/util/Utils.ts @@ -98,37 +98,42 @@ async function expireOldPresenceStates() { } export async function handleOffloadedGatewayRequest(socket: WebSocket, url: string, body: unknown): Promise { - // TODO: async json object streaming - const resp = await fetch(url, { - body: JSON.stringify(body), - method: "POST", - headers: { - Authorization: `Bearer ${socket.accessToken}`, - // because the session may not have an id in the token! - "X-Session-Id": socket.session_id, - "Content-Type": "application/json", - }, - }); + try { + // TODO: async json object streaming + const resp = await fetch(url, { + body: JSON.stringify(body), + method: "POST", + headers: { + Authorization: `Bearer ${socket.accessToken}`, + // because the session may not have an id in the token! + "X-Session-Id": socket.session_id, + "Content-Type": "application/json", + }, + }); - if (!resp.ok) { - const text = await resp.text(); - console.error(`[Gateway] Offloaded request to ${url} failed with status ${resp.status}: ${text}`); - if (resp.status === 415 || resp.status === 400) console.log(typeof body, body); - // throw new Error(`Offloaded request failed with status ${resp.status}: ${text}`); + if (!resp.ok) { + const text = await resp.text(); + console.error(`[Gateway] Offloaded request to ${url} failed with status ${resp.status}: ${text}`); + if (resp.status === 415 || resp.status === 400) console.log(typeof body, body); + // throw new Error(`Offloaded request failed with status ${resp.status}: ${text}`); + return false; + } + + const data = ((await resp.json()) as Event[]).toReversed(); + while (data.length > 0) { + const event = data.pop()!; + if (process.env.WS_VERBOSE) console.log(`[Gateway] Received offloaded event: ${JSON.stringify(event)}`); + await Send(socket, { + op: OPCODES.Dispatch, + s: socket.sequence++, + t: event.event, + d: event.data, + }); + } + + return true; + } catch (e) { + console.error("Error while handling offloaded gateway request:", e); return false; } - - const data = ((await resp.json()) as Event[]).toReversed(); - while (data.length > 0) { - const event = data.pop()!; - if (process.env.WS_VERBOSE) console.log(`[Gateway] Received offloaded event: ${JSON.stringify(event)}`); - await Send(socket, { - op: OPCODES.Dispatch, - s: socket.sequence++, - t: event.event, - d: event.data, - }); - } - - return true; }