Skip to content

Commit 3494fda

Browse files
authored
fix(core): add exception handling to migrateFromFileStorage (google-gemini#27229)
1 parent 1a024f3 commit 3494fda

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

packages/core/src/code_assist/oauth-credential-storage.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,18 @@ describe('OAuthCredentialStorage', () => {
180180
expect(result).toEqual(mockCredentials);
181181
});
182182

183-
it('should throw an error if the migration file contains invalid JSON', async () => {
183+
it('should return null and log a warning if the migration file contains invalid JSON', async () => {
184184
vi.spyOn(mockHybridTokenStorage, 'getCredentials').mockResolvedValue(
185185
null,
186186
);
187187
vi.spyOn(fs, 'readFile').mockResolvedValue('invalid json');
188188

189-
await expect(OAuthCredentialStorage.loadCredentials()).rejects.toThrow(
190-
'Failed to load OAuth credentials',
189+
const result = await OAuthCredentialStorage.loadCredentials();
190+
191+
expect(result).toBeNull();
192+
expect(coreEvents.emitFeedback).toHaveBeenCalledWith(
193+
'warning',
194+
expect.stringContaining('Corrupted OAuth credential file'),
191195
);
192196
});
193197

packages/core/src/code_assist/oauth-credential-storage.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,17 @@ export class OAuthCredentialStorage {
129129
throw error;
130130
}
131131

132-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
133-
const credentials: Credentials = JSON.parse(credsJson);
132+
let credentials: Credentials;
133+
try {
134+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
135+
credentials = JSON.parse(credsJson);
136+
} catch {
137+
coreEvents.emitFeedback(
138+
'warning',
139+
`Corrupted OAuth credential file at ${oldFilePath}, skipping migration`,
140+
);
141+
return null;
142+
}
134143

135144
// Save to new storage
136145
await this.saveCredentials(credentials);

0 commit comments

Comments
 (0)