Skip to content

Commit

Permalink
fix(compass-global-writes): try/catch and show error when global-writ…
Browse files Browse the repository at this point in the history
…es 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]>
  • Loading branch information
djechlin-mongodb and paula-stacho authored Nov 7, 2024
1 parent 1aabedc commit 636a9c4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
25 changes: 24 additions & 1 deletion packages/compass-global-writes/src/store/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,28 @@ function createStore({
hasShardingError = () => false,
hasShardKey = () => false,
failsOnShardingRequest = () => false,
failsOnShardZoneRequest = () => false,
authenticatedFetchStub,
}:
| {
isNamespaceManaged?: () => boolean;
hasShardingError?: () => boolean;
hasShardKey?: () => boolean | AtlasShardKey;
failsOnShardingRequest?: () => boolean;
failsOnShardZoneRequest?: () => boolean;
authenticatedFetchStub?: never;
}
| {
isNamespaceManaged?: never;
hasShardingError?: never;
hasShardKey?: () => boolean | ShardKey;
failsOnShardingRequest?: never;
failsOnShardZoneRequest?: () => never;
authenticatedFetchStub?: () => void;
} = {}): GlobalWritesStore {
const atlasService = {
authenticatedFetch: (uri: string) => {
if (uri.endsWith(`/geoSharding`) && failsOnShardingRequest()) {
if (uri.endsWith('/geoSharding') && failsOnShardingRequest()) {
return Promise.reject(new Error('Failed to shard'));
}

Expand All @@ -112,6 +115,13 @@ function createStore({
});
}

if (
/geoSharding.*newFormLocationMapping/.test(uri) &&
failsOnShardZoneRequest()
) {
return Promise.reject(new Error('Failed to fetch shard zones'));
}

return createAuthFetchResponse({});
},
automationAgentRequest: (_meta: unknown, type: string) => ({
Expand Down Expand Up @@ -266,6 +276,7 @@ describe('GlobalWritesStore Store', function () {
const store = createStore({
isNamespaceManaged: () => true,
hasShardKey: Sinon.fake(() => mockShardKey),
failsOnShardZoneRequest: () => true,
});
await waitFor(() => {
expect(store.getState().status).to.equal('SHARDING');
Expand Down Expand Up @@ -313,6 +324,18 @@ describe('GlobalWritesStore Store', function () {
});
});

it('valid shard key -> failsOnShardZoneRequest', async function () {
const store = createStore({
isNamespaceManaged: () => true,
hasShardKey: () => true,
failsOnShardZoneRequest: () => true,
});
await waitFor(() => {
expect(store.getState().status).to.equal('SHARD_KEY_CORRECT');
expect(store.getState().managedNamespace).to.equal(managedNamespace);
});
});

it('incomplete setup -> sharding -> shard key correct', async function () {
// initial state -> incomplete shardingSetup
clock = sinon.useFakeTimers({
Expand Down
50 changes: 43 additions & 7 deletions packages/compass-global-writes/src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum GlobalWritesActionTypes {
NamespaceShardKeyFetched = 'global-writes/NamespaceShardKeyFetched',

ShardZonesFetched = 'global-writes/ShardZonesFetched',
ShardZonesFetchedError = 'global-writes/ShardZonesFetchedError',

SubmittingForShardingStarted = 'global-writes/SubmittingForShardingStarted',
SubmittingForShardingFinished = 'global-writes/SubmittingForShardingFinished',
Expand Down Expand Up @@ -67,6 +68,10 @@ type ShardZonesFetchedAction = {
shardZones: ShardZoneData[];
};

type ShardZonesFetchedErrorAction = {
type: GlobalWritesActionTypes.ShardZonesFetchedError;
};

type SubmittingForShardingStartedAction = {
type: GlobalWritesActionTypes.SubmittingForShardingStarted;
};
Expand Down Expand Up @@ -350,6 +355,18 @@ const reducer: Reducer<RootState, Action> = (state = initialState, action) => {
};
}

if (
isAction<ShardZonesFetchedErrorAction>(
action,
GlobalWritesActionTypes.ShardZonesFetchedError
)
) {
return {
...state,
shardZones: [],
};
}

if (
isAction<SubmittingForShardingStartedAction>(
action,
Expand Down Expand Up @@ -880,18 +897,37 @@ export const fetchNamespaceShardKey = (): GlobalWritesThunkAction<

export const fetchShardingZones = (): GlobalWritesThunkAction<
Promise<void>,
ShardZonesFetchedAction
ShardZonesFetchedAction | ShardZonesFetchedErrorAction
> => {
return async (dispatch, getState, { atlasGlobalWritesService }) => {
return async (
dispatch,
getState,
{ atlasGlobalWritesService, connectionInfoRef }
) => {
const { shardZones } = getState();
if (shardZones.length > 0) {
return;
}
const shardingZones = await atlasGlobalWritesService.getShardingZones();
dispatch({
type: GlobalWritesActionTypes.ShardZonesFetched,
shardZones: shardingZones,
});
try {
const shardingZones = await atlasGlobalWritesService.getShardingZones();
dispatch({
type: GlobalWritesActionTypes.ShardZonesFetched,
shardZones: shardingZones,
});
} catch (error) {
dispatch({
type: GlobalWritesActionTypes.ShardZonesFetchedError,
});
openToast(
`global-writes-fetch-sharding-zones-error-${connectionInfoRef.current.id}`,
{
title: `Failed to fetch sharding zones: ${(error as Error).message}`,
dismissible: true,
timeout: 5000,
variant: 'important',
}
);
}
};
};

Expand Down

0 comments on commit 636a9c4

Please sign in to comment.