From ff1d2bd5c34c8ed4cf0c2670c5faa084d29fcf70 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Tue, 13 Jun 2023 22:58:20 +0200 Subject: [PATCH 1/3] throw error for failed ai response --- packages/core/streams/ai-stream.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/core/streams/ai-stream.ts b/packages/core/streams/ai-stream.ts index 89fecf0..4e01225 100644 --- a/packages/core/streams/ai-stream.ts +++ b/packages/core/streams/ai-stream.ts @@ -90,6 +90,16 @@ export function AIStream( customParser: AIStreamParser, callbacks?: AIStreamCallbacks ): ReadableStream { + // If the response is not 200, we want to throw an error to indicate that + // the AI service is not available. + // When catching this error, we can check the status code and return a handled + // error response to the client. + if (res.status !== 200) { + throw new Error( + `Failed to convert the response to stream. Received status code: ${res.status}.` + ) + } + const stream = res.body || new ReadableStream({ From d1dd1da00bb381170132e998a6f4e4187e3f8d69 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Tue, 13 Jun 2023 22:59:20 +0200 Subject: [PATCH 2/3] add changeset --- .changeset/tidy-onions-camp.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/tidy-onions-camp.md diff --git a/.changeset/tidy-onions-camp.md b/.changeset/tidy-onions-camp.md new file mode 100644 index 0000000..0e1ff68 --- /dev/null +++ b/.changeset/tidy-onions-camp.md @@ -0,0 +1,5 @@ +--- +'ai-connector': patch +--- + +Throw error when provided AI response isn't valid From 28eafe862ac655fff29092fdf76a0aad53298e16 Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Tue, 13 Jun 2023 23:06:40 +0200 Subject: [PATCH 3/3] use .ok instead --- packages/core/streams/ai-stream.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/streams/ai-stream.ts b/packages/core/streams/ai-stream.ts index 4e01225..53ab211 100644 --- a/packages/core/streams/ai-stream.ts +++ b/packages/core/streams/ai-stream.ts @@ -90,11 +90,11 @@ export function AIStream( customParser: AIStreamParser, callbacks?: AIStreamCallbacks ): ReadableStream { - // If the response is not 200, we want to throw an error to indicate that + // If the response is not OK, we want to throw an error to indicate that // the AI service is not available. // When catching this error, we can check the status code and return a handled // error response to the client. - if (res.status !== 200) { + if (!res.ok) { throw new Error( `Failed to convert the response to stream. Received status code: ${res.status}.` )