From f82b6e4ba9bf527b3a4c17872162d9ae124ead0d Mon Sep 17 00:00:00 2001 From: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com> Date: Wed, 14 Aug 2024 16:17:32 +0200 Subject: [PATCH] fix: Add better error handling for chat errors (#10408) --- packages/editor-ui/src/utils/apiUtils.ts | 60 +++++++++++++----------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/packages/editor-ui/src/utils/apiUtils.ts b/packages/editor-ui/src/utils/apiUtils.ts index 64f6ec259..e9201b7c3 100644 --- a/packages/editor-ui/src/utils/apiUtils.ts +++ b/packages/editor-ui/src/utils/apiUtils.ts @@ -4,6 +4,7 @@ import { ApplicationError, jsonParse, type GenericValue, type IDataObject } from import type { IExecutionFlattedResponse, IExecutionResponse, IRestApiContext } from '@/Interface'; import { parse } from 'flatted'; import type { ChatRequest } from '@/types/assistant.types'; +import { assert } from '@/utils/assert'; const BROWSER_ID_STORAGE_KEY = 'n8n-browserId'; let browserId = localStorage.getItem(BROWSER_ID_STORAGE_KEY); @@ -214,41 +215,46 @@ export const streamRequest = async ( credentials: 'include', body: JSON.stringify(payload), }; - const response = await fetch(`${context.baseUrl}${apiEndpoint}`, assistantRequest); + try { + const response = await fetch(`${context.baseUrl}${apiEndpoint}`, assistantRequest); - if (response.ok && response.body) { - // Handle the streaming response - const reader = response.body.getReader(); - const decoder = new TextDecoder('utf-8'); + if (response.ok && response.body) { + // Handle the streaming response + const reader = response.body.getReader(); + const decoder = new TextDecoder('utf-8'); - async function readStream() { - const { done, value } = await reader.read(); - if (done) { - onDone?.(); - return; - } + async function readStream() { + const { done, value } = await reader.read(); + if (done) { + onDone?.(); + return; + } - const chunk = decoder.decode(value); - const splitChunks = chunk.split(separator); + const chunk = decoder.decode(value); + const splitChunks = chunk.split(separator); - for (const splitChunk of splitChunks) { - if (splitChunk && onChunk) { - try { - onChunk(jsonParse(splitChunk, { errorMessage: 'Invalid json chunk in stream' })); - } catch (e: unknown) { - if (e instanceof Error) { - console.log(`${e.message}: ${splitChunk}`); - onError?.(e); + for (const splitChunk of splitChunks) { + if (splitChunk && onChunk) { + try { + onChunk(jsonParse(splitChunk, { errorMessage: 'Invalid json chunk in stream' })); + } catch (e: unknown) { + if (e instanceof Error) { + console.log(`${e.message}: ${splitChunk}`); + onError?.(e); + } } } } + await readStream(); } - await readStream(); - } - // Start reading the stream - await readStream(); - } else if (onError) { - onError(new Error(response.statusText)); + // Start reading the stream + await readStream(); + } else if (onError) { + onError(new Error(response.statusText)); + } + } catch (e: unknown) { + assert(e instanceof Error); + onError?.(e); } };