@@ -14,7 +14,7 @@ import { slotsService } from './slots';
1414 * - Manages streaming and non-streaming response parsing
1515 * - Provides request abortion capabilities
1616 * - Converts database messages to API format
17- * - Handles error translation and context detection
17+ * - Handles error translation for server responses
1818 *
1919 * - **ChatStore**: Stateful orchestration and UI state management
2020 * - Uses ChatService for all AI model communication
@@ -27,7 +27,6 @@ import { slotsService } from './slots';
2727 * - Streaming response handling with real-time callbacks
2828 * - Reasoning content extraction and processing
2929 * - File attachment processing (images, PDFs, audio, text)
30- * - Context error detection and reporting
3130 * - Request lifecycle management (abort, cleanup)
3231 */
3332export class ChatService {
@@ -215,10 +214,13 @@ export class ChatService {
215214 userFriendlyError = new Error (
216215 'Unable to connect to server - please check if the server is running'
217216 ) ;
217+ userFriendlyError . name = 'NetworkError' ;
218218 } else if ( error . message . includes ( 'ECONNREFUSED' ) ) {
219219 userFriendlyError = new Error ( 'Connection refused - server may be offline' ) ;
220+ userFriendlyError . name = 'NetworkError' ;
220221 } else if ( error . message . includes ( 'ETIMEDOUT' ) ) {
221- userFriendlyError = new Error ( 'Request timeout - server may be overloaded' ) ;
222+ userFriendlyError = new Error ( 'TCP Timeout' ) ;
223+ userFriendlyError . name = 'TimeoutError' ;
222224 } else {
223225 userFriendlyError = error ;
224226 }
@@ -325,12 +327,8 @@ export class ChatService {
325327
326328 if ( streamFinished ) {
327329 if ( ! hasReceivedData && aggregatedContent . length === 0 ) {
328- const contextError = new Error (
329- 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
330- ) ;
331- contextError . name = 'ContextError' ;
332- onError ?.( contextError ) ;
333- return ;
330+ const noResponseError = new Error ( 'No response received from server. Please try again.' ) ;
331+ throw noResponseError ;
334332 }
335333
336334 onComplete ?.( aggregatedContent , fullReasoningContent || undefined , lastTimings ) ;
@@ -369,12 +367,8 @@ export class ChatService {
369367 const responseText = await response . text ( ) ;
370368
371369 if ( ! responseText . trim ( ) ) {
372- const contextError = new Error (
373- 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
374- ) ;
375- contextError . name = 'ContextError' ;
376- onError ?.( contextError ) ;
377- throw contextError ;
370+ const noResponseError = new Error ( 'No response received from server. Please try again.' ) ;
371+ throw noResponseError ;
378372 }
379373
380374 const data : ApiChatCompletionResponse = JSON . parse ( responseText ) ;
@@ -386,22 +380,14 @@ export class ChatService {
386380 }
387381
388382 if ( ! content . trim ( ) ) {
389- const contextError = new Error (
390- 'The request exceeds the available context size. Try increasing the context size or enable context shift.'
391- ) ;
392- contextError . name = 'ContextError' ;
393- onError ?.( contextError ) ;
394- throw contextError ;
383+ const noResponseError = new Error ( 'No response received from server. Please try again.' ) ;
384+ throw noResponseError ;
395385 }
396386
397387 onComplete ?.( content , reasoningContent ) ;
398388
399389 return content ;
400390 } catch ( error ) {
401- if ( error instanceof Error && error . name === 'ContextError' ) {
402- throw error ;
403- }
404-
405391 const err = error instanceof Error ? error : new Error ( 'Parse error' ) ;
406392
407393 onError ?.( err ) ;
@@ -595,37 +581,19 @@ export class ChatService {
595581 const errorText = await response . text ( ) ;
596582 const errorData : ApiErrorResponse = JSON . parse ( errorText ) ;
597583
598- if ( errorData . error ?. type === 'exceed_context_size_error' ) {
599- const contextError = errorData . error as ApiContextSizeError ;
600- const error = new Error ( contextError . message ) ;
601- error . name = 'ContextError' ;
602- // Attach structured context information
603- (
604- error as Error & {
605- contextInfo ?: { promptTokens : number ; maxContext : number ; estimatedTokens : number } ;
606- }
607- ) . contextInfo = {
608- promptTokens : contextError . n_prompt_tokens ,
609- maxContext : contextError . n_ctx ,
610- estimatedTokens : contextError . n_prompt_tokens
611- } ;
612- return error ;
613- }
614-
615- // Fallback for other error types
616584 const message = errorData . error ?. message || 'Unknown server error' ;
617- return new Error ( message ) ;
585+ const error = new Error ( message ) ;
586+ error . name = response . status === 400 ? 'ServerError' : 'HttpError' ;
587+
588+ return error ;
618589 } catch {
619590 // If we can't parse the error response, return a generic error
620- return new Error ( `Server error (${ response . status } ): ${ response . statusText } ` ) ;
591+ const fallback = new Error ( `Server error (${ response . status } ): ${ response . statusText } ` ) ;
592+ fallback . name = 'HttpError' ;
593+ return fallback ;
621594 }
622595 }
623596
624- /**
625- * Updates the processing state with timing information from the server response
626- * @param timings - Timing data from the API response
627- * @param promptProgress - Progress data from the API response
628- */
629597 private updateProcessingState (
630598 timings ?: ChatMessageTimings ,
631599 promptProgress ?: ChatMessagePromptProgress
0 commit comments