From fe52238ac2735dbf5a8670619693ae867ce51237 Mon Sep 17 00:00:00 2001 From: jialin Date: Wed, 30 Apr 2025 12:25:10 +0800 Subject: [PATCH] chore: error handler in stream response --- src/hooks/use-chunk-fetch.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/hooks/use-chunk-fetch.ts b/src/hooks/use-chunk-fetch.ts index b80ee3dc..be5cb2bd 100644 --- a/src/hooks/use-chunk-fetch.ts +++ b/src/hooks/use-chunk-fetch.ts @@ -20,10 +20,25 @@ interface RequestConfig { contentType?: 'json' | 'text'; } +async function parseErrorResponse(response: Response) { + try { + const contentType = response.headers.get('content-type') || ''; + if (contentType.includes('application/json')) { + return await response.json(); + } else { + const text = await response.text(); + return { message: text || 'Unknown error' }; + } + } catch (e) { + return { message: 'Failed to parse error response' }; + } +} + const useSetChunkFetch = () => { const axiosToken = useRef(null); const requestConfig = useRef({}); const chunkDataRef = useRef([]); + const readTextEventStreamData = async ( response: Response, callback: HandlerFunction, @@ -103,7 +118,7 @@ const useSetChunkFetch = () => { bufferManager.add(chunk); throttledCallback(); } catch (error) { - console.log('error============:', error); + // handle error } } }; @@ -134,7 +149,7 @@ const useSetChunkFetch = () => { ); if (!response.ok) { - const error = await response.json(); + const error = await parseErrorResponse(response); if (errorHandler) { errorHandler(error); } else { @@ -144,8 +159,6 @@ const useSetChunkFetch = () => { } await readTextEventStreamData(response, handler); - - console.log('chunkDataRef.current===1', chunkDataRef.current); } catch (error) { // handle error: catched in request interceptor }