@@ -6,6 +6,43 @@ import { getSolutionStream, getFollowUpStream, getGeneralStream } from './ai'
66import { state } from './state'
77import { settings } from './settings'
88
9+ /**
10+ * Extract meaningful error message from API errors
11+ */
12+ function extractErrorMessage ( error : unknown ) : string {
13+ if ( ! ( error instanceof Error ) ) {
14+ return String ( error ) || '未知错误'
15+ }
16+
17+ // Try to extract responseBody from AI SDK errors
18+ const apiError = error as Error & {
19+ responseBody ?: string
20+ statusCode ?: number
21+ data ?: unknown
22+ }
23+
24+ // Try to parse responseBody for detailed message
25+ if ( apiError . responseBody ) {
26+ try {
27+ const body = JSON . parse ( apiError . responseBody )
28+ if ( body . message ) {
29+ return body . message
30+ }
31+ if ( body . error ?. message ) {
32+ return body . error . message
33+ }
34+ } catch {
35+ // If parsing fails, use responseBody as is
36+ if ( typeof apiError . responseBody === 'string' && apiError . responseBody . length < 200 ) {
37+ return apiError . responseBody
38+ }
39+ }
40+ }
41+
42+ // Fallback to error message
43+ return error . message || '未知错误'
44+ }
45+
946type Shortcut = {
1047 action : string
1148 key : string
@@ -153,8 +190,7 @@ const callbacks: Record<string, () => void> = {
153190 if ( ! streamContext . controller . signal . aborted ) {
154191 endedNaturally = false
155192 console . error ( 'Error streaming solution:' , error )
156- const errorMessage = error instanceof Error ? error . message : 'Unknown error'
157- mainWindow . webContents . send ( 'solution-error' , errorMessage )
193+ mainWindow . webContents . send ( 'solution-error' , extractErrorMessage ( error ) )
158194 } else {
159195 endedNaturally = false
160196 }
@@ -181,9 +217,8 @@ const callbacks: Record<string, () => void> = {
181217 }
182218 } else {
183219 endedNaturally = false
184- const errorMessage = error instanceof Error ? error . message : 'Unknown error'
185220 console . error ( 'Error streaming solution:' , error )
186- mainWindow . webContents . send ( 'solution-error' , errorMessage )
221+ mainWindow . webContents . send ( 'solution-error' , extractErrorMessage ( error ) )
187222 }
188223 } finally {
189224 if ( currentStreamContext === streamContext ) {
@@ -272,8 +307,7 @@ const callbacks: Record<string, () => void> = {
272307 if ( ! streamContext . controller . signal . aborted ) {
273308 endedNaturally = false
274309 console . error ( 'Error streaming continuous solution:' , error )
275- const errorMessage = error instanceof Error ? error . message : 'Unknown error'
276- mainWindow . webContents . send ( 'solution-error' , errorMessage )
310+ mainWindow . webContents . send ( 'solution-error' , extractErrorMessage ( error ) )
277311 } else {
278312 endedNaturally = false
279313 }
@@ -300,9 +334,8 @@ const callbacks: Record<string, () => void> = {
300334 }
301335 } else {
302336 endedNaturally = false
303- const errorMessage = error instanceof Error ? error . message : 'Unknown error'
304337 console . error ( 'Error streaming continuous solution:' , error )
305- mainWindow . webContents . send ( 'solution-error' , errorMessage )
338+ mainWindow . webContents . send ( 'solution-error' , extractErrorMessage ( error ) )
306339 }
307340 } finally {
308341 if ( currentStreamContext === streamContext ) {
@@ -499,8 +532,7 @@ ipcMain.handle('sendFollowUpQuestion', async (_event, question: string) => {
499532 if ( ! streamContext . controller . signal . aborted ) {
500533 endedNaturally = false
501534 console . error ( 'Error streaming follow-up solution:' , error )
502- const errorMessage = error instanceof Error ? error . message : 'Unknown error'
503- mainWindow . webContents . send ( 'solution-error' , errorMessage )
535+ mainWindow . webContents . send ( 'solution-error' , extractErrorMessage ( error ) )
504536 } else {
505537 endedNaturally = false
506538 }
@@ -536,9 +568,8 @@ ipcMain.handle('sendFollowUpQuestion', async (_event, question: string) => {
536568 }
537569 } else {
538570 endedNaturally = false
539- const errorMessage = error instanceof Error ? error . message : 'Unknown error'
540571 console . error ( 'Error streaming follow-up solution:' , error )
541- mainWindow . webContents . send ( 'solution-error' , errorMessage )
572+ mainWindow . webContents . send ( 'solution-error' , extractErrorMessage ( error ) )
542573 }
543574 } finally {
544575 if ( currentStreamContext === streamContext ) {
0 commit comments