fix(core): avoid error handling on cancelled requests to prevent crash (#6039)
Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
parent
63f9e86bc3
commit
f8f79bf2f7
|
@ -445,6 +445,32 @@ describe('Turn', () => {
|
||||||
{ type: GeminiEventType.Finished, value: 'OTHER' },
|
{ 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', () => {
|
describe('getDebugResponses', () => {
|
||||||
|
|
|
@ -247,16 +247,17 @@ export class Turn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const error = toFriendlyError(e);
|
|
||||||
if (error instanceof UnauthorizedError) {
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
if (signal.aborted) {
|
if (signal.aborted) {
|
||||||
yield { type: GeminiEventType.UserCancelled };
|
yield { type: GeminiEventType.UserCancelled };
|
||||||
// Regular cancellation error, fail gracefully.
|
// Regular cancellation error, fail gracefully.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const error = toFriendlyError(e);
|
||||||
|
if (error instanceof UnauthorizedError) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
const contextForReport = [...this.chat.getHistory(/*curated*/ true), req];
|
const contextForReport = [...this.chat.getHistory(/*curated*/ true), req];
|
||||||
await reportError(
|
await reportError(
|
||||||
error,
|
error,
|
||||||
|
|
Loading…
Reference in New Issue