Skip to content

Commit 1411b85

Browse files
fix!: onError not called if cacheFlags is true and api is not reachable (#365)
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e15a0dc commit 1411b85

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

flagsmith-core.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ const Flagsmith = class {
516516
}
517517
} catch (e) {
518518
this.log("Exception fetching cached logs", e);
519+
throw e;
519520
}
520521
} else {
521522
if (!preventFetch) {
@@ -540,7 +541,15 @@ const Flagsmith = class {
540541
try {
541542
const res = AsyncStorage.getItemSync? AsyncStorage.getItemSync(this.getStorageKey()) : await AsyncStorage.getItem(this.getStorageKey());
542543
await onRetrievedStorage(null, res)
543-
} catch (e) {}
544+
} catch (e) {
545+
// Only re-throw if we don't have fallback flags (defaultFlags or cached flags)
546+
if (!this.flags || Object.keys(this.flags).length === 0) {
547+
throw e;
548+
}
549+
// We have fallback flags, so call onError but don't reject init()
550+
const typedError = e instanceof Error ? e : new Error(`${e}`);
551+
this.onError?.(typedError);
552+
}
544553
}
545554
} else if (!preventFetch) {
546555
await this.getFlags();

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "9.3.5",
3+
"version": "10.0.0",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"module": "./index.mjs",

lib/react-native-flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "9.3.5",
3+
"version": "10.0.0",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

test/init.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,21 @@ describe('Flagsmith.init', () => {
274274
});
275275
expect(onError).toHaveBeenCalledWith(new Error('Mocked fetch error'));
276276
});
277+
test('should call onError when the API cannot be reached with cacheFlags enabled but no cache exists', async () => {
278+
const onError = jest.fn();
279+
const { flagsmith, initConfig } = getFlagsmith({
280+
cacheFlags: true,
281+
fetch: async () => {
282+
return Promise.resolve({ text: () => Promise.resolve('Mocked fetch error'), ok: false, status: 401 });
283+
},
284+
onError,
285+
});
286+
// NOTE: No AsyncStorage.setItem() - cache is empty, and no defaultFlags provided
287+
288+
await expect(flagsmith.init(initConfig)).rejects.toThrow('Mocked fetch error');
289+
expect(onError).toHaveBeenCalledTimes(1);
290+
expect(onError).toHaveBeenCalledWith(new Error('Mocked fetch error'));
291+
});
277292
test('should send app name and version headers when provided', async () => {
278293
const onChange = jest.fn();
279294
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({

utils/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// Auto-generated by write-version.js
2-
export const SDK_VERSION = "9.3.2";
2+
export const SDK_VERSION = "10.0.0";

0 commit comments

Comments
 (0)