File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ import { postShareV1ChatFeedback } from '@/request/ShareChat';
1111import { getShareV1ConversationDetail } from '@/request/ShareConversation' ;
1212import { postShareV1CommonFileUpload } from '@/request/ShareFile' ;
1313import { copyText } from '@/utils' ;
14- import SSEClient from '@/utils/fetch' ;
14+ import SSEClient , { SSEHttpError } from '@/utils/fetch' ;
1515import { Image as ImagePreview , message } from '@ctzhian/ui' ;
1616import CloseIcon from '@mui/icons-material/Close' ;
1717import ExpandMoreIcon from '@mui/icons-material/ExpandMore' ;
@@ -615,6 +615,16 @@ const AiQaContent: React.FC<{
615615 headers : {
616616 'Content-Type' : 'application/json' ,
617617 } ,
618+ onError : error => {
619+ setLoading ( false ) ;
620+ setThinking ( 4 ) ;
621+ if ( error instanceof SSEHttpError && error . status === 401 ) {
622+ const current = window . location ;
623+ window . location . href = `${ basePath } /auth/login?redirect=${ encodeURIComponent ( current . pathname + current . search ) } ` ;
624+ return ;
625+ }
626+ message . error ( error . message || '请求失败' ) ;
627+ } ,
618628 onCancel : ( ) => {
619629 setLoading ( false ) ;
620630 setThinking ( 4 ) ;
Original file line number Diff line number Diff line change @@ -2,6 +2,15 @@ type SSECallback<T> = (data: T) => void;
22type SSEErrorCallback = ( error : Error ) => void ;
33type SSECompleteCallback = ( ) => void ;
44
5+ export class SSEHttpError extends Error {
6+ status : number ;
7+ constructor ( status : number , message : string ) {
8+ super ( message ) ;
9+ this . name = 'SSEHttpError' ;
10+ this . status = status ;
11+ }
12+ }
13+
514interface SSEClientOptions {
615 url : string ;
716 headers ?: Record < string , string > ;
@@ -63,7 +72,13 @@ class SSEClient<T> {
6372 . then ( async response => {
6473 if ( ! response . ok ) {
6574 clearTimeout ( timeoutId ) ;
66- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
75+ let errorMessage = `HTTP error! status: ${ response . status } ` ;
76+ try {
77+ const body = await response . json ( ) ;
78+ if ( body ?. message ) errorMessage = body . message ;
79+ else if ( body ?. error ) errorMessage = body . error ;
80+ } catch { }
81+ throw new SSEHttpError ( response . status , errorMessage ) ;
6782 }
6883 if ( ! response . body ) {
6984 clearTimeout ( timeoutId ) ;
You can’t perform that action at this time.
0 commit comments