Skip to content

Commit 29432cf

Browse files
committed
feat(connections): add onFailToLoadConnections callback for error handling
1 parent 375a2b4 commit 29432cf

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

configs/testing-library-compass/src/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,12 @@ function createWrapper(
330330
<ConnectFnProvider connect={wrapperState.connect}>
331331
<CompassConnections
332332
appName={options.appName ?? 'TEST'}
333+
onFailToLoadConnections={
334+
options.onFailToLoadConnections ??
335+
(() => {
336+
// noop
337+
})
338+
}
333339
onExtraConnectionDataRequest={
334340
options.onExtraConnectionDataRequest ??
335341
(() => {

packages/compass-connections/src/index.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ const ConnectionsComponent: React.FunctionComponent<{
6363
* connections on plugin activate
6464
*/
6565
preloadStorageConnectionInfos?: ConnectionInfo[];
66+
/**
67+
* When connections fail to load, this callback will be called
68+
*/
69+
onFailToLoadConnections: (error: Error) => void;
6670
}> = ({ children }) => {
6771
return (
6872
<ConnectionActionsProvider>
@@ -90,6 +94,7 @@ const CompassConnectionsPlugin = registerHadronPlugin(
9094
appName: initialProps.appName,
9195
connectFn: initialProps.connectFn,
9296
globalAppRegistry,
97+
onFailToLoadConnections: initialProps.onFailToLoadConnections,
9398
});
9499

95100
setTimeout(() => {

packages/compass-connections/src/stores/connections-store-redux.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ type ThunkExtraArg = {
216216
) => Promise<[ExtraConnectionDataForTelemetry, string | null]>;
217217
connectFn?: typeof devtoolsConnect;
218218
globalAppRegistry: Pick<AppRegistry, 'on' | 'emit' | 'removeListener'>;
219+
onFailToLoadConnections: (error: Error) => void;
219220
};
220221

221222
export type ConnectionsThunkAction<
@@ -1266,7 +1267,11 @@ export const loadConnections = (): ConnectionsThunkAction<
12661267
| ConnectionsLoadSuccessAction
12671268
| ConnectionsLoadErrorAction
12681269
> => {
1269-
return async (dispatch, getState, { connectionStorage }) => {
1270+
return async (
1271+
dispatch,
1272+
getState,
1273+
{ connectionStorage, onFailToLoadConnections }
1274+
) => {
12701275
if (getState().connections.status !== 'initial') {
12711276
return;
12721277
}
@@ -1276,6 +1281,7 @@ export const loadConnections = (): ConnectionsThunkAction<
12761281
dispatch({ type: ActionTypes.ConnectionsLoadSuccess, connections });
12771282
} catch (err) {
12781283
dispatch({ type: ActionTypes.ConnectionsLoadError, error: err as any });
1284+
onFailToLoadConnections(err as Error);
12791285
}
12801286
};
12811287
};

packages/compass-web/sandbox/index.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import React, { useLayoutEffect, useRef } from 'react';
1+
import React, { useCallback, useLayoutEffect, useRef } from 'react';
22
import ReactDOM from 'react-dom';
3-
import { resetGlobalCSS, css, Body } from '@mongodb-js/compass-components';
3+
import {
4+
resetGlobalCSS,
5+
css,
6+
Body,
7+
openToast,
8+
} from '@mongodb-js/compass-components';
49
import type { AllPreferences } from 'compass-preferences-model';
510
import { CompassWeb } from '../src/index';
611
import { SandboxConnectionStorageProvider } from '../src/connection-storage';
@@ -86,6 +91,14 @@ const App = () => {
8691
getMetaEl('csrf-time').setAttribute('content', csrfTime ?? '');
8792
}, [csrfToken, csrfTime]);
8893

94+
const onFailToLoadConnections = useCallback((error: Error) => {
95+
openToast('failed-to-load-connections', {
96+
title: 'Failed to load connections',
97+
description: error.message,
98+
variant: 'warning',
99+
});
100+
}, []);
101+
89102
if (status === 'checking') {
90103
return null;
91104
}
@@ -132,6 +145,7 @@ const App = () => {
132145
onTrack={sandboxTelemetry.track}
133146
onDebug={sandboxLogger.debug}
134147
onLog={sandboxLogger.log}
148+
onFailToLoadConnections={onFailToLoadConnections}
135149
></CompassWeb>
136150
</Body>
137151
</SandboxPreferencesUpdateProvider>

packages/compass-web/src/entrypoint.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ type CompassWebProps = {
150150
onOpenConnectViaModal?: (
151151
atlasMetadata: ConnectionInfo['atlasMetadata']
152152
) => void;
153+
154+
/**
155+
* Callback prop called when connections fail to load
156+
*/
157+
onFailToLoadConnections: (err: Error) => void;
153158
};
154159

155160
function CompassWorkspace({
@@ -253,6 +258,7 @@ const CompassWeb = ({
253258
onDebug,
254259
onTrack,
255260
onOpenConnectViaModal,
261+
onFailToLoadConnections,
256262
}: CompassWebProps) => {
257263
const appRegistry = useRef(new AppRegistry());
258264
const logger = useCompassWebLoggerAndTelemetry({
@@ -335,6 +341,7 @@ const CompassWeb = ({
335341
>
336342
<CompassConnections
337343
appName={appName ?? 'Compass Web'}
344+
onFailToLoadConnections={onFailToLoadConnections}
338345
onExtraConnectionDataRequest={() => {
339346
return Promise.resolve([{}, null] as [
340347
Record<string, unknown>,

packages/compass/src/app/components/home.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
css,
77
cx,
88
getScrollbarStyles,
9+
openToast,
910
palette,
1011
resetGlobalCSS,
1112
} from '@mongodb-js/compass-components';
@@ -149,6 +150,13 @@ function HomeWithConnections({
149150
onExtraConnectionDataRequest={getExtraConnectionData}
150151
onAutoconnectInfoRequest={onAutoconnectInfoRequest}
151152
doNotReconnectDisconnectedAutoconnectInfo
153+
onFailToLoadConnections={(error) => {
154+
openToast('failed-to-load-connections', {
155+
title: 'Failed to load connections',
156+
description: error.message,
157+
variant: 'warning',
158+
});
159+
}}
152160
>
153161
<Home {...props}></Home>
154162
</CompassConnections>

0 commit comments

Comments
 (0)