From 60e9f8ca0d6224889768264fb210071da42457c4 Mon Sep 17 00:00:00 2001 From: Chase Naples Date: Wed, 24 Sep 2025 21:53:20 -0400 Subject: [PATCH] Preserve API connection error message --- src/client.ts | 2 +- tests/lib/ChatCompletionRunFunctions.test.ts | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/client.ts b/src/client.ts index b3c899b44..357e14231 100644 --- a/src/client.ts +++ b/src/client.ts @@ -653,7 +653,7 @@ export class OpenAI { if (isTimeout) { throw new Errors.APIConnectionTimeoutError(); } - throw new Errors.APIConnectionError({ cause: response }); + throw new Errors.APIConnectionError({ message: response.message, cause: response }); } const specialHeaders = [...response.headers.entries()] diff --git a/tests/lib/ChatCompletionRunFunctions.test.ts b/tests/lib/ChatCompletionRunFunctions.test.ts index f14257ccd..e73651d3d 100644 --- a/tests/lib/ChatCompletionRunFunctions.test.ts +++ b/tests/lib/ChatCompletionRunFunctions.test.ts @@ -2407,11 +2407,15 @@ describe('resource completions', () => { throw new Error('mock request error'); }).catch(() => {}); - async function runStream() { - await stream.done(); - } - - await expect(runStream).rejects.toThrow(APIConnectionError); + await expect(async () => { + try { + await stream.done(); + } catch (err) { + expect(err).toBeInstanceOf(APIConnectionError); + expect((err as Error).message).toBe('mock request error'); + throw err; + } + }).rejects.toThrow('mock request error'); }); test('handles network errors on async iterator', async () => { const { fetch, handleRequest } = mockFetch();