Skip to content

Commit

Permalink
Call onError if API request fails
Browse files Browse the repository at this point in the history
  • Loading branch information
frankieyan committed Oct 10, 2024
1 parent db9e757 commit 02f7110
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
21 changes: 16 additions & 5 deletions flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const Flagsmith = class {
}
}

getFlags = () => {
getFlags = ({ callOnErrorWhenRequestFails = false }: { callOnErrorWhenRequestFails?: boolean } = {}) => {
let { api, evaluationContext } = this;
this.log("Get Flags")
this.isLoading = true;
Expand Down Expand Up @@ -210,12 +210,24 @@ const Flagsmith = class {
return handleResponse(res?.[0] as IFlagsmithResponse | null)
}).catch(({ message }) => {
const error = new Error(message)
return Promise.reject(error)

if (callOnErrorWhenRequestFails) {
this.onError?.(error)
} else {
return Promise.reject(error)
}
});
} else {
return this.getJSON(api + "flags/")
.then((res) => {
return handleResponse({ flags: res as IFlagsmithResponse['flags'], traits:undefined })
}).catch((error) => {

if (callOnErrorWhenRequestFails) {
this.onError?.(error)
} else {
throw error
}
})
}
};
Expand Down Expand Up @@ -479,9 +491,8 @@ const Flagsmith = class {
}
if (shouldFetchFlags) {
// We want to resolve init since we have cached flags
this.getFlags().catch((e) => {
this.log("Exception fetching cached logs", e);
});

this.getFlags({ callOnErrorWhenRequestFails: true })
}
} else {
if (!preventFetch) {
Expand Down
10 changes: 9 additions & 1 deletion test/init.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Sample test
import { waitFor } from '@testing-library/react';
import { defaultState, environmentID, getFlagsmith, getStateToCheck, identityState } from './test-constants';
import { promises as fs } from 'fs'

Expand Down Expand Up @@ -149,17 +150,24 @@ describe('Flagsmith.init', () => {
})}),
)
});
test('should not reject when the API cannot be reached but the cache is populated', async () => {
test('should not reject but call onError, when the API cannot be reached with the cache populated', async () => {
const onError = jest.fn()
const { flagsmith, initConfig, AsyncStorage } = getFlagsmith({
cacheFlags: true,
fetch: async () => {
return Promise.resolve({ text: () => Promise.resolve('Mocked fetch error'), ok: false, status: 401 });
},
onError,
});
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify(defaultState));
await flagsmith.init(initConfig);

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

await waitFor(() => {
expect(onError).toHaveBeenCalledTimes(1)
expect(onError).toHaveBeenCalledWith(new Error('Mocked fetch error'))
})
})
test('should not reject when the API cannot be reached but default flags are set', async () => {
const { flagsmith, initConfig } = getFlagsmith({
Expand Down

0 comments on commit 02f7110

Please sign in to comment.