Skip to content

Commit

Permalink
fix(session replay): add error handling for store (#892)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxiwang authored Oct 8, 2024
2 parents 07d0b25 + 8cc09b7 commit b53061d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/session-replay-browser/src/events/events-idb-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,15 @@ export class SessionReplayEventsIDBStore implements AmplitudeSessionReplayEvents
}

async initialize(type: EventType, sessionId?: number) {
const dbSuffix = type === 'replay' ? '' : `_${type}`;
const dbName = `${this.apiKey.substring(0, 10)}_amp_session_replay_events${dbSuffix}`;
this.db = await createStore(dbName);
this.timeAtLastSplit = Date.now(); // Initialize this so we have a point of comparison when events are recorded
await this.transitionFromKeyValStore(sessionId);
try {
const dbSuffix = type === 'replay' ? '' : `_${type}`;
const dbName = `${this.apiKey.substring(0, 10)}_amp_session_replay_events${dbSuffix}`;
this.db = await createStore(dbName);
this.timeAtLastSplit = Date.now(); // Initialize this so we have a point of comparison when events are recorded
await this.transitionFromKeyValStore(sessionId);
} catch (e) {
this.loggerProvider.warn(`${STORAGE_FAILURE}: ${e as string}`);
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions packages/session-replay-browser/test/events-idb-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ describe('SessionReplayEventsIDBStore', () => {
await eventsStorage.initialize('replay');
expect(EventsIDBStore.createStore).toHaveBeenCalledWith('static_key_amp_session_replay_events');
});

test('should catch errors', async () => {
const mockCreateStore = jest.spyOn(EventsIDBStore, 'createStore');
const errorMessage = 'Failed to create store';

mockCreateStore.mockRejectedValue(new Error(errorMessage));

const eventsStorage = new SessionReplayEventsIDBStore({ apiKey, loggerProvider: mockLoggerProvider });

await eventsStorage.initialize('replay');
expect(mockLoggerProvider.warn).toHaveBeenCalled();
});
});

describe('getSequencesToSend', () => {
Expand Down

0 comments on commit b53061d

Please sign in to comment.