From 6236529fbedb668a2b66b563734ba0877fe44ba2 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Fri, 6 Jun 2025 10:45:10 -0700 Subject: [PATCH] update premature close message --- packages/fetch/src/premature-close.ts | 14 -------------- packages/fetch/src/stream.ts | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 18 deletions(-) delete mode 100644 packages/fetch/src/premature-close.ts diff --git a/packages/fetch/src/premature-close.ts b/packages/fetch/src/premature-close.ts deleted file mode 100644 index dd03b5a83..000000000 --- a/packages/fetch/src/premature-close.ts +++ /dev/null @@ -1,14 +0,0 @@ -export function getPrematureCloseErrorMessage(chunks: number): string { - if (chunks === 0) { - return "Connection was closed before any data was received (Premature close). Try again."; - } else { - return ( - "Stream was cancelled mid-stream. This can happen for various reasons, including:\n" + - "- Malformed chunks of data received from the server\n" + - "- The server closed the connection before sending the complete response\n" + - "- Long delays from the server during streaming\n" + - "- 'Keep alive' header being used in combination with an http agent and a set, low number of maxSockets\n" + - "The underlying node fetch error is 'Premature Close'" - ); - } -} diff --git a/packages/fetch/src/stream.ts b/packages/fetch/src/stream.ts index 3310b639f..190b0166c 100644 --- a/packages/fetch/src/stream.ts +++ b/packages/fetch/src/stream.ts @@ -1,5 +1,3 @@ -import { getPrematureCloseErrorMessage } from "./premature-close.js"; - export async function* toAsyncIterable( nodeReadable: NodeJS.ReadableStream, ): AsyncGenerator { @@ -54,10 +52,22 @@ export async function* streamResponse( return; // In case of client-side cancellation, just return } if (e.message.toLowerCase().includes("premature close")) { - throw new Error(getPrematureCloseErrorMessage(chunks)); + // Premature close can happen for various reasons, including: + // - Malformed chunks of data received from the server + // - The server closed the connection before sending the complete response + // - Long delays from the server during streaming + // - 'Keep alive' header being used in combination with an http agent and a set, low number of maxSockets + if (chunks === 0) { + throw new Error( + "Stream was closed before any data was received. Try again. (Premature Close)", + ); + } else { + throw new Error( + "The response was cancelled mid-stream. Try again. (Premature Close).", + ); + } } } - throw e; } }