Skip to content

Commit

Permalink
Added error handling if flush occurs before initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
aklarfeld committed Apr 25, 2024
1 parent ffa416d commit 3f589f6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const errors = {
NO_CLIENT_ID:
'No Client ID Provided, set SUPERGOOD_CLIENT_ID or pass it as an argument',
NO_CLIENT_SECRET:
'No Client Secret Provided, set SUPERGOOD_CLIENT_SECRET or pass it as an argument'
'No Client Secret Provided, set SUPERGOOD_CLIENT_SECRET or pass it as an argument',
};

const SensitiveKeyActions = {
Expand Down
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ const Supergood = () => {

// Force flush cache means don't wait for responses
const flushCache = async ({ force } = { force: false }) => {

if(!responseCache || !requestCache) {
return;
}

const responseCacheKeys = responseCache.keys();
const requestCacheKeys = requestCache.keys();
const responseCacheValues = Object.values(responseCache.mget(responseCacheKeys));
Expand Down Expand Up @@ -403,11 +408,11 @@ const Supergood = () => {
clearInterval(remoteConfigFetchInterval);

// If there are hanging requests, wait a second
if (requestCache.keys().length > 0) {
if (requestCache?.keys().length > 0) {
await sleep(supergoodConfig.waitAfterClose);
}

interceptor.teardown();
interceptor?.teardown();
await flushCache({ force });
return false;
};
Expand All @@ -416,7 +421,7 @@ const Supergood = () => {
// If the request cache isn't empty, this means that
// there are responses that are still being processed.
// Wait for them to finish before flushing the cache.
if (requestCache.keys().length > 0) {
if (requestCache?.keys().length > 0) {
await sleep(supergoodConfig.waitAfterClose);
}

Expand Down
8 changes: 8 additions & 0 deletions test/e2e/core.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,12 @@ describe('core functionality', () => {
expect(postEventsMock).not.toHaveBeenCalled();
});
});

describe('clean error handling', () => {
it('should not crash if a flush is called before an init', async () => {
await Supergood.close();
await Supergood.waitAndFlushCache();
expect(postEventsMock).not.toHaveBeenCalled();
});
});
});

0 comments on commit 3f589f6

Please sign in to comment.