mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix: Add better error handling for chat errors (#10408)
This commit is contained in:
parent
80c96a3dcc
commit
f82b6e4ba9
|
@ -4,6 +4,7 @@ import { ApplicationError, jsonParse, type GenericValue, type IDataObject } from
|
||||||
import type { IExecutionFlattedResponse, IExecutionResponse, IRestApiContext } from '@/Interface';
|
import type { IExecutionFlattedResponse, IExecutionResponse, IRestApiContext } from '@/Interface';
|
||||||
import { parse } from 'flatted';
|
import { parse } from 'flatted';
|
||||||
import type { ChatRequest } from '@/types/assistant.types';
|
import type { ChatRequest } from '@/types/assistant.types';
|
||||||
|
import { assert } from '@/utils/assert';
|
||||||
|
|
||||||
const BROWSER_ID_STORAGE_KEY = 'n8n-browserId';
|
const BROWSER_ID_STORAGE_KEY = 'n8n-browserId';
|
||||||
let browserId = localStorage.getItem(BROWSER_ID_STORAGE_KEY);
|
let browserId = localStorage.getItem(BROWSER_ID_STORAGE_KEY);
|
||||||
|
@ -214,41 +215,46 @@ export const streamRequest = async (
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
body: JSON.stringify(payload),
|
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) {
|
if (response.ok && response.body) {
|
||||||
// Handle the streaming response
|
// Handle the streaming response
|
||||||
const reader = response.body.getReader();
|
const reader = response.body.getReader();
|
||||||
const decoder = new TextDecoder('utf-8');
|
const decoder = new TextDecoder('utf-8');
|
||||||
|
|
||||||
async function readStream() {
|
async function readStream() {
|
||||||
const { done, value } = await reader.read();
|
const { done, value } = await reader.read();
|
||||||
if (done) {
|
if (done) {
|
||||||
onDone?.();
|
onDone?.();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunk = decoder.decode(value);
|
const chunk = decoder.decode(value);
|
||||||
const splitChunks = chunk.split(separator);
|
const splitChunks = chunk.split(separator);
|
||||||
|
|
||||||
for (const splitChunk of splitChunks) {
|
for (const splitChunk of splitChunks) {
|
||||||
if (splitChunk && onChunk) {
|
if (splitChunk && onChunk) {
|
||||||
try {
|
try {
|
||||||
onChunk(jsonParse(splitChunk, { errorMessage: 'Invalid json chunk in stream' }));
|
onChunk(jsonParse(splitChunk, { errorMessage: 'Invalid json chunk in stream' }));
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
if (e instanceof Error) {
|
if (e instanceof Error) {
|
||||||
console.log(`${e.message}: ${splitChunk}`);
|
console.log(`${e.message}: ${splitChunk}`);
|
||||||
onError?.(e);
|
onError?.(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
await readStream();
|
||||||
}
|
}
|
||||||
await readStream();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start reading the stream
|
// Start reading the stream
|
||||||
await readStream();
|
await readStream();
|
||||||
} else if (onError) {
|
} else if (onError) {
|
||||||
onError(new Error(response.statusText));
|
onError(new Error(response.statusText));
|
||||||
|
}
|
||||||
|
} catch (e: unknown) {
|
||||||
|
assert(e instanceof Error);
|
||||||
|
onError?.(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue