fix(core): add exception handling to migrateFromFileStorage (#27229)

This commit is contained in:
Dev Randalpura
2026-05-19 10:10:21 -05:00
committed by GitHub
parent 1a024f30a3
commit 3494fda2cf
2 changed files with 18 additions and 5 deletions

View File

@@ -180,14 +180,18 @@ describe('OAuthCredentialStorage', () => {
expect(result).toEqual(mockCredentials);
});
it('should throw an error if the migration file contains invalid JSON', async () => {
it('should return null and log a warning if the migration file contains invalid JSON', async () => {
vi.spyOn(mockHybridTokenStorage, 'getCredentials').mockResolvedValue(
null,
);
vi.spyOn(fs, 'readFile').mockResolvedValue('invalid json');
await expect(OAuthCredentialStorage.loadCredentials()).rejects.toThrow(
'Failed to load OAuth credentials',
const result = await OAuthCredentialStorage.loadCredentials();
expect(result).toBeNull();
expect(coreEvents.emitFeedback).toHaveBeenCalledWith(
'warning',
expect.stringContaining('Corrupted OAuth credential file'),
);
});

View File

@@ -129,8 +129,17 @@ export class OAuthCredentialStorage {
throw error;
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const credentials: Credentials = JSON.parse(credsJson);
let credentials: Credentials;
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
credentials = JSON.parse(credsJson);
} catch {
coreEvents.emitFeedback(
'warning',
`Corrupted OAuth credential file at ${oldFilePath}, skipping migration`,
);
return null;
}
// Save to new storage
await this.saveCredentials(credentials);