Skip to content

Commit

Permalink
fix: Handle uncaught fetch exception
Browse files Browse the repository at this point in the history
  • Loading branch information
frankieyan committed Oct 3, 2024
1 parent 3d0e8b8 commit 7b0f640
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ const Flagsmith = class {
}
if (shouldFetchFlags) {
// We want to resolve init since we have cached flags
this.getFlags();
await this.getFlags();
}
} else {
if (!preventFetch) {
Expand Down
24 changes: 24 additions & 0 deletions test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,28 @@ describe('Flagsmith.init', () => {
})}),
)
});
test('should not reject when the API cannot be reached but the cache is populated', async () => {
const { flagsmith, initConfig, AsyncStorage } = getFlagsmith({
cacheFlags: true,
fetch: async () => {
return Promise.resolve({ text: () => Promise.resolve('Mocked fetch error'), ok: false, status: 401 });
},
});
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify(defaultState));
await flagsmith.init(initConfig);

expect(getStateToCheck(flagsmith.getState())).toEqual(defaultState);
})
test('should not reject when the API cannot be reached but default flags are set', async () => {
const { flagsmith, initConfig } = getFlagsmith({
defaultFlags: defaultState.flags,
cacheFlags: true,
fetch: async () => {
return Promise.resolve({ text: () => Promise.resolve('Mocked fetch error'), ok: false, status: 401 });
},
});
await flagsmith.init(initConfig);

expect(getStateToCheck(flagsmith.getState())).toEqual(defaultState);
})
});

0 comments on commit 7b0f640

Please sign in to comment.