@@ -13,7 +13,7 @@ import { slotsService } from './slots';
1313 * - Manages streaming and non-streaming response parsing
1414 * - Provides request abortion capabilities
1515 * - Converts database messages to API format
16- * - Handles error translation and context detection
16+ * - Handles error translation for server responses
1717 *
1818 * - **ChatStore**: Stateful orchestration and UI state management
1919 * - Uses ChatService for all AI model communication
@@ -26,7 +26,6 @@ import { slotsService } from './slots';
2626 * - Streaming response handling with real-time callbacks
2727 * - Reasoning content extraction and processing
2828 * - File attachment processing (images, PDFs, audio, text)
29- * - Context error detection and reporting
3029 * - Request lifecycle management (abort, cleanup)
3130 */
3231export class ChatService {
@@ -209,10 +208,13 @@ export class ChatService {
209208 userFriendlyError = new Error (
210209 'Unable to connect to server - please check if the server is running'
211210 ) ;
211+ userFriendlyError . name = 'NetworkError' ;
212212 } else if ( error . message . includes ( 'ECONNREFUSED' ) ) {
213213 userFriendlyError = new Error ( 'Connection refused - server may be offline' ) ;
214+ userFriendlyError . name = 'NetworkError' ;
214215 } else if ( error . message . includes ( 'ETIMEDOUT' ) ) {
215- userFriendlyError = new Error ( 'Request timeout - server may be overloaded' ) ;
216+ userFriendlyError = new Error ( 'TCP Timeout' ) ;
217+ userFriendlyError . name = 'TimeoutError' ;
216218 } else {
217219 userFriendlyError = error ;
218220 }
@@ -319,12 +321,8 @@ export class ChatService {
319321
320322 if ( streamFinished ) {
321323 if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
322- const contextError = new Error (
323- 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
324- ) ;
325- contextError . name = 'ContextError' ;
326- onError ?.( contextError ) ;
327- return ;
324+ const noResponseError = new Error ( 'No response received from server. Please try again.' ) ;
325+ throw noResponseError ;
328326 }
329327
330328 onComplete ?.( aggregatedContent , fullReasoningContent || undefined , lastTimings ) ;
@@ -363,12 +361,8 @@ export class ChatService {
363361 const responseText = await response . text ( ) ;
364362
365363 if ( ! responseText . trim ( ) ) {
366- const contextError = new Error (
367- 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
368- ) ;
369- contextError . name = 'ContextError' ;
370- onError ?.( contextError ) ;
371- throw contextError ;
364+ const noResponseError = new Error ( 'No response received from server. Please try again.' ) ;
365+ throw noResponseError ;
372366 }
373367
374368 const data : ApiChatCompletionResponse = JSON . parse ( responseText ) ;
@@ -380,22 +374,14 @@ export class ChatService {
380374 }
381375
382376 if ( ! content . trim ( ) ) {
383- const contextError = new Error (
384- 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
385- ) ;
386- contextError . name = 'ContextError' ;
387- onError ?.( contextError ) ;
388- throw contextError ;
377+ const noResponseError = new Error ( 'No response received from server. Please try again.' ) ;
378+ throw noResponseError ;
389379 }
390380
391381 onComplete ?.( content , reasoningContent ) ;
392382
393383 return content ;
394384 } catch ( error ) {
395- if ( error instanceof Error && error . name === 'ContextError' ) {
396- throw error ;
397- }
398-
399385 const err = error instanceof Error ? error : new Error ( 'Parse error' ) ;
400386
401387 onError ?.( err ) ;
@@ -589,37 +575,19 @@ export class ChatService {
589575 const errorText = await response . text ( ) ;
590576 const errorData : ApiErrorResponse = JSON . parse ( errorText ) ;
591577
592- if ( errorData . error ?. type === 'exceed_context_size_error' ) {
593- const contextError = errorData . error as ApiContextSizeError ;
594- const error = new Error ( contextError . message ) ;
595- error . name = 'ContextError' ;
596- // Attach structured context information
597- (
598- error as Error & {
599- contextInfo ?: { promptTokens : number ; maxContext : number ; estimatedTokens : number } ;
600- }
601- ) . contextInfo = {
602- promptTokens : contextError . n_prompt_tokens ,
603- maxContext : contextError . n_ctx ,
604- estimatedTokens : contextError . n_prompt_tokens
605- } ;
606- return error ;
607- }
608-
609- // Fallback for other error types
610578 const message = errorData . error ?. message || 'Unknown server error' ;
611- return new Error ( message ) ;
579+ const error = new Error ( message ) ;
580+ error . name = response . status === 400 ? 'ServerError' : 'HttpError' ;
581+
582+ return error ;
612583 } catch {
613584 // If we can't parse the error response, return a generic error
614- return new Error ( `Server error (${ response . status } ): ${ response . statusText } ` ) ;
585+ const fallback = new Error ( `Server error (${ response . status } ): ${ response . statusText } ` ) ;
586+ fallback . name = 'HttpError' ;
587+ return fallback ;
615588 }
616589 }
617590
618- /**
619- * Updates the processing state with timing information from the server response
620- * @param timings - Timing data from the API response
621- * @param promptProgress - Progress data from the API response
622- */
623591 private updateProcessingState (
624592 timings ?: ChatMessageTimings ,
625593 promptProgress ?: ChatMessagePromptProgress
0 commit comments