diff options
| author | pwrwpw <[email protected]> | 2025-08-21 16:18:30 +0900 | 
|---|---|---|
| committer | GitHub <[email protected]> | 2025-08-21 07:18:30 +0000 | 
| commit | f8f79bf2f772e27ffe4af968679137b734a92dd3 (patch) | |
| tree | c01d18ef0fd3c0aaef88140efa50f034554cd218 | |
| parent | 63f9e86bc3b7c7007be1a73f0cadb59d8bbd1fdc (diff) | |
fix(core): avoid error handling on cancelled requests to prevent crash (#6039)
Co-authored-by: Jacob Richman <[email protected]>
| -rw-r--r-- | packages/core/src/core/turn.test.ts | 26 | ||||
| -rw-r--r-- | packages/core/src/core/turn.ts | 9 | 
2 files changed, 31 insertions, 4 deletions
diff --git a/packages/core/src/core/turn.test.ts b/packages/core/src/core/turn.test.ts index 7144d16b..595de151 100644 --- a/packages/core/src/core/turn.test.ts +++ b/packages/core/src/core/turn.test.ts @@ -445,6 +445,32 @@ describe('Turn', () => {          { type: GeminiEventType.Finished, value: 'OTHER' },        ]);      }); + +    it('should not crash when cancelled request has malformed error', async () => { +      const abortController = new AbortController(); + +      const errorToThrow = { +        response: { +          data: undefined, // Malformed error data +        }, +      }; + +      mockSendMessageStream.mockImplementation(async () => { +        abortController.abort(); +        throw errorToThrow; +      }); + +      const events = []; +      const reqParts: Part[] = [{ text: 'Test malformed error handling' }]; + +      for await (const event of turn.run(reqParts, abortController.signal)) { +        events.push(event); +      } + +      expect(events).toEqual([{ type: GeminiEventType.UserCancelled }]); + +      expect(reportError).not.toHaveBeenCalled(); +    });    });    describe('getDebugResponses', () => { diff --git a/packages/core/src/core/turn.ts b/packages/core/src/core/turn.ts index 8ede1fa4..5f2f017e 100644 --- a/packages/core/src/core/turn.ts +++ b/packages/core/src/core/turn.ts @@ -247,16 +247,17 @@ export class Turn {          }        }      } catch (e) { -      const error = toFriendlyError(e); -      if (error instanceof UnauthorizedError) { -        throw error; -      }        if (signal.aborted) {          yield { type: GeminiEventType.UserCancelled };          // Regular cancellation error, fail gracefully.          return;        } +      const error = toFriendlyError(e); +      if (error instanceof UnauthorizedError) { +        throw error; +      } +        const contextForReport = [...this.chat.getHistory(/*curated*/ true), req];        await reportError(          error,  | 
