Skip to content

Commit 636a9c4

Browse files
fix(compass-global-writes): try/catch and show error when global-writes fetch fails COMPASS-8333 (#6408)
* changes * remove throw error * start tests * tests * Update packages/compass-global-writes/src/store/reducer.ts Co-authored-by: Paula Stachova <[email protected]> * reformat --------- Co-authored-by: Paula Stachova <[email protected]>
1 parent 1aabedc commit 636a9c4

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

packages/compass-global-writes/src/store/index.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,28 @@ function createStore({
7272
hasShardingError = () => false,
7373
hasShardKey = () => false,
7474
failsOnShardingRequest = () => false,
75+
failsOnShardZoneRequest = () => false,
7576
authenticatedFetchStub,
7677
}:
7778
| {
7879
isNamespaceManaged?: () => boolean;
7980
hasShardingError?: () => boolean;
8081
hasShardKey?: () => boolean | AtlasShardKey;
8182
failsOnShardingRequest?: () => boolean;
83+
failsOnShardZoneRequest?: () => boolean;
8284
authenticatedFetchStub?: never;
8385
}
8486
| {
8587
isNamespaceManaged?: never;
8688
hasShardingError?: never;
8789
hasShardKey?: () => boolean | ShardKey;
8890
failsOnShardingRequest?: never;
91+
failsOnShardZoneRequest?: () => never;
8992
authenticatedFetchStub?: () => void;
9093
} = {}): GlobalWritesStore {
9194
const atlasService = {
9295
authenticatedFetch: (uri: string) => {
93-
if (uri.endsWith(`/geoSharding`) && failsOnShardingRequest()) {
96+
if (uri.endsWith('/geoSharding') && failsOnShardingRequest()) {
9497
return Promise.reject(new Error('Failed to shard'));
9598
}
9699

@@ -112,6 +115,13 @@ function createStore({
112115
});
113116
}
114117

118+
if (
119+
/geoSharding.*newFormLocationMapping/.test(uri) &&
120+
failsOnShardZoneRequest()
121+
) {
122+
return Promise.reject(new Error('Failed to fetch shard zones'));
123+
}
124+
115125
return createAuthFetchResponse({});
116126
},
117127
automationAgentRequest: (_meta: unknown, type: string) => ({
@@ -266,6 +276,7 @@ describe('GlobalWritesStore Store', function () {
266276
const store = createStore({
267277
isNamespaceManaged: () => true,
268278
hasShardKey: Sinon.fake(() => mockShardKey),
279+
failsOnShardZoneRequest: () => true,
269280
});
270281
await waitFor(() => {
271282
expect(store.getState().status).to.equal('SHARDING');
@@ -313,6 +324,18 @@ describe('GlobalWritesStore Store', function () {
313324
});
314325
});
315326

327+
it('valid shard key -> failsOnShardZoneRequest', async function () {
328+
const store = createStore({
329+
isNamespaceManaged: () => true,
330+
hasShardKey: () => true,
331+
failsOnShardZoneRequest: () => true,
332+
});
333+
await waitFor(() => {
334+
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
335+
expect(store.getState().managedNamespace).to.equal(managedNamespace);
336+
});
337+
});
338+
316339
it('incomplete setup -> sharding -> shard key correct', async function () {
317340
// initial state -> incomplete shardingSetup
318341
clock = sinon.useFakeTimers({

packages/compass-global-writes/src/store/reducer.ts

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ enum GlobalWritesActionTypes {
3030
NamespaceShardKeyFetched = 'global-writes/NamespaceShardKeyFetched',
3131

3232
ShardZonesFetched = 'global-writes/ShardZonesFetched',
33+
ShardZonesFetchedError = 'global-writes/ShardZonesFetchedError',
3334

3435
SubmittingForShardingStarted = 'global-writes/SubmittingForShardingStarted',
3536
SubmittingForShardingFinished = 'global-writes/SubmittingForShardingFinished',
@@ -67,6 +68,10 @@ type ShardZonesFetchedAction = {
6768
shardZones: ShardZoneData[];
6869
};
6970

71+
type ShardZonesFetchedErrorAction = {
72+
type: GlobalWritesActionTypes.ShardZonesFetchedError;
73+
};
74+
7075
type SubmittingForShardingStartedAction = {
7176
type: GlobalWritesActionTypes.SubmittingForShardingStarted;
7277
};
@@ -350,6 +355,18 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
350355
};
351356
}
352357

358+
if (
359+
isAction<ShardZonesFetchedErrorAction>(
360+
action,
361+
GlobalWritesActionTypes.ShardZonesFetchedError
362+
)
363+
) {
364+
return {
365+
...state,
366+
shardZones: [],
367+
};
368+
}
369+
353370
if (
354371
isAction<SubmittingForShardingStartedAction>(
355372
action,
@@ -880,18 +897,37 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<
880897

881898
export const fetchShardingZones = (): GlobalWritesThunkAction<
882899
Promise<void>,
883-
ShardZonesFetchedAction
900+
ShardZonesFetchedAction | ShardZonesFetchedErrorAction
884901
> => {
885-
return async (dispatch, getState, { atlasGlobalWritesService }) => {
902+
return async (
903+
dispatch,
904+
getState,
905+
{ atlasGlobalWritesService, connectionInfoRef }
906+
) => {
886907
const { shardZones } = getState();
887908
if (shardZones.length > 0) {
888909
return;
889910
}
890-
const shardingZones = await atlasGlobalWritesService.getShardingZones();
891-
dispatch({
892-
type: GlobalWritesActionTypes.ShardZonesFetched,
893-
shardZones: shardingZones,
894-
});
911+
try {
912+
const shardingZones = await atlasGlobalWritesService.getShardingZones();
913+
dispatch({
914+
type: GlobalWritesActionTypes.ShardZonesFetched,
915+
shardZones: shardingZones,
916+
});
917+
} catch (error) {
918+
dispatch({
919+
type: GlobalWritesActionTypes.ShardZonesFetchedError,
920+
});
921+
openToast(
922+
`global-writes-fetch-sharding-zones-error-${connectionInfoRef.current.id}`,
923+
{
924+
title: `Failed to fetch sharding zones: ${(error as Error).message}`,
925+
dismissible: true,
926+
timeout: 5000,
927+
variant: 'important',
928+
}
929+
);
930+
}
895931
};
896932
};
897933

0 commit comments

Comments
 (0)