Skip to content

Commit 171bc4b

Browse files
committed
chore: handle removing item from storage gracefully
1 parent 83decfb commit 171bc4b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/OidcAuthStateStorage.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import EncryptedStorage from 'react-native-encrypted-storage';
22
import OidcAuthStateStorage, {
33
type OidcAuthState,
44
} from './OidcAuthStateStorage';
5+
import * as SentryIntegrationModule from './sentryIntegration';
56

67
describe('OidcAuthStateStorage', () => {
78
const CLIENT_ID = 'test-client-id';
89
const EXPECTED_STORAGE_KEY = `de.contentpass.${CLIENT_ID}-OIDCAuthState`;
910

1011
let storage: OidcAuthStateStorage;
12+
let reportErrorSpy: jest.SpyInstance;
1113
const mockAuthState: OidcAuthState = {
1214
accessToken: 'test-access-token',
1315
accessTokenExpirationDate: '2023-12-31T23:59:59Z',
@@ -18,6 +20,9 @@ describe('OidcAuthStateStorage', () => {
1820

1921
beforeEach(() => {
2022
storage = new OidcAuthStateStorage(CLIENT_ID);
23+
reportErrorSpy = jest
24+
.spyOn(SentryIntegrationModule, 'reportError')
25+
.mockReturnValue(undefined);
2126
});
2227

2328
afterEach(() => {
@@ -59,4 +64,18 @@ describe('OidcAuthStateStorage', () => {
5964
EXPECTED_STORAGE_KEY
6065
);
6166
});
67+
68+
it('should report error if clearing OIDC auth state fails', async () => {
69+
const error = new Error('test-error');
70+
(EncryptedStorage.removeItem as jest.Mock).mockRejectedValue(error);
71+
72+
await storage.clearOidcAuthState();
73+
74+
expect(EncryptedStorage.removeItem).toHaveBeenCalledWith(
75+
EXPECTED_STORAGE_KEY
76+
);
77+
expect(reportErrorSpy).toHaveBeenCalledWith(error, {
78+
msg: 'Failed to clear OIDC auth state. Most probably we tried to remove item which does not exist',
79+
});
80+
});
6281
});

src/OidcAuthStateStorage.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import EncryptedStorage from 'react-native-encrypted-storage';
2+
import { reportError } from './sentryIntegration';
23

34
const AUTH_STATE_KEY = 'OIDCAuthState';
45

@@ -28,6 +29,12 @@ export default class OidcAuthStateStorage {
2829
}
2930

3031
public async clearOidcAuthState() {
31-
await EncryptedStorage.removeItem(this.key);
32+
try {
33+
await EncryptedStorage.removeItem(this.key);
34+
} catch (err: any) {
35+
reportError(err, {
36+
msg: 'Failed to clear OIDC auth state. Most probably we tried to remove item which does not exist',
37+
});
38+
}
3239
}
3340
}

0 commit comments

Comments
 (0)