Skip to content

Commit 0205fb4

Browse files
committed
use error banner instead of toast
1 parent d7f9ceb commit 0205fb4

File tree

3 files changed

+73
-48
lines changed

3 files changed

+73
-48
lines changed

packages/compass-schema/src/components/compass-schema.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ import { getAtlasPerformanceAdvisorLink } from '../utils';
3636
import { useIsLastAppliedQueryOutdated } from '@mongodb-js/compass-query-bar';
3737
import { useTelemetry } from '@mongodb-js/compass-telemetry/provider';
3838
import type { RootState } from '../stores/store';
39-
import { startAnalysis, stopAnalysis } from '../stores/schema-analysis-reducer';
39+
import {
40+
type SchemaAnalysisError,
41+
startAnalysis,
42+
stopAnalysis,
43+
} from '../stores/schema-analysis-reducer';
4044
import { openExportSchema } from '../stores/schema-export-reducer';
4145
import ExportSchemaModal from './export-schema-modal';
4246
import ExportSchemaLegacyBanner from './export-schema-legacy-banner';
@@ -371,7 +375,7 @@ const PerformanceAdvisorBanner = () => {
371375

372376
const Schema: React.FunctionComponent<{
373377
analysisState: AnalysisState;
374-
errorMessage?: string;
378+
error?: SchemaAnalysisError;
375379
maxTimeMS?: number;
376380
schema: MongodbSchema | null;
377381
count?: number;
@@ -381,7 +385,7 @@ const Schema: React.FunctionComponent<{
381385
onStopAnalysis: () => void;
382386
}> = ({
383387
analysisState,
384-
errorMessage,
388+
error,
385389
schema,
386390
resultId,
387391
onExportSchemaClicked,
@@ -410,7 +414,7 @@ const Schema: React.FunctionComponent<{
410414
onExportSchemaClicked={onExportSchemaClicked}
411415
onResetClicked={onApplyClicked}
412416
analysisState={analysisState}
413-
errorMessage={errorMessage || ''}
417+
error={error}
414418
isOutdated={!!outdated}
415419
sampleSize={schema ? schema.count : 0}
416420
schemaResultId={resultId || ''}
@@ -440,7 +444,7 @@ const Schema: React.FunctionComponent<{
440444
export default connect(
441445
(state: RootState) => ({
442446
analysisState: state.schemaAnalysis.analysisState,
443-
errorMessage: state.schemaAnalysis.errorMessage,
447+
error: state.schemaAnalysis.error,
444448
schema: state.schemaAnalysis.schema,
445449
resultId: state.schemaAnalysis.resultId,
446450
}),

packages/compass-schema/src/components/schema-toolbar/schema-toolbar.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { usePreference } from 'compass-preferences-model/provider';
1313
import type { AnalysisState } from '../../constants/analysis-states';
1414
import { ANALYSIS_STATE_COMPLETE } from '../../constants/analysis-states';
1515
import { QueryBar } from '@mongodb-js/compass-query-bar';
16+
import { type SchemaAnalysisError } from '../../stores/schema-analysis-reducer';
1617

1718
const schemaToolbarStyles = css({
1819
display: 'flex',
@@ -43,6 +44,13 @@ const schemaToolbarActionBarRightStyles = css({
4344
paddingLeft: spacing[2],
4445
});
4546

47+
const ERROR_WARNING = 'An error occurred during schema analysis';
48+
const COMPLEXITY_ABORT_MESSAGE = `
49+
Analysis was aborted due to: Fields count above 1000.
50+
Consider breaking up your data into more collections with smaller documents, and using references to consolidate the data you need.
51+
`;
52+
const INCREASE_MAX_TIME_MS_HINT_MESSAGE =
53+
'Operation exceeded time limit. Please try increasing the maxTimeMS for the query in the filter options.';
4654
const OUTDATED_WARNING_MESSAGE =
4755
'The schema content is outdated and no longer in sync' +
4856
' with the documents view. Press "Analyze" again to see the schema for the' +
@@ -53,7 +61,7 @@ const SCHEMA_ANALYSIS_DOCS_LINK =
5361

5462
type SchemaToolbarProps = {
5563
analysisState: AnalysisState;
56-
errorMessage: string;
64+
error: SchemaAnalysisError;
5765
isOutdated: boolean;
5866
onAnalyzeSchemaClicked: () => void;
5967
onExportSchemaClicked: () => void;
@@ -64,7 +72,7 @@ type SchemaToolbarProps = {
6472

6573
const SchemaToolbar: React.FunctionComponent<SchemaToolbarProps> = ({
6674
analysisState,
67-
errorMessage,
75+
error,
6876
isOutdated,
6977
onAnalyzeSchemaClicked,
7078
onExportSchemaClicked,
@@ -123,6 +131,21 @@ const SchemaToolbar: React.FunctionComponent<SchemaToolbarProps> = ({
123131
</div>
124132
</div>
125133
)}
134+
{error?.errorType === 'GENERAL' && (
135+
<ErrorSummary
136+
data-testid="schema-toolbar-error-message"
137+
errors={[`${ERROR_WARNING}: ${error.errorMessage}`]}
138+
/>
139+
)}
140+
{error?.errorType === 'TIMEOUT' && (
141+
<WarningSummary warnings={[INCREASE_MAX_TIME_MS_HINT_MESSAGE]} />
142+
)}
143+
{error?.errorType === 'HIGH_COMPLEXITY' && (
144+
<ErrorSummary
145+
data-testid="schema-toolbar-complexity-abort-message"
146+
errors={[COMPLEXITY_ABORT_MESSAGE]}
147+
/>
148+
)}
126149
{analysisState === ANALYSIS_STATE_COMPLETE && isOutdated && (
127150
<WarningSummary warnings={[OUTDATED_WARNING_MESSAGE]} />
128151
)}

packages/compass-schema/src/stores/schema-analysis-reducer.ts

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import type { Schema } from 'mongodb-schema';
22
import { isInternalFieldPath } from 'hadron-document';
3-
import {
4-
openToast,
5-
type ToastProperties,
6-
} from '@mongodb-js/compass-components';
73
import type { Action, Reducer } from 'redux';
8-
import type { AggregateOptions } from 'mongodb';
4+
import type { AggregateOptions, MongoError } from 'mongodb';
95
import type { QueryBarService } from '@mongodb-js/compass-query-bar';
106
import { type AnalysisState } from '../constants/analysis-states';
117
import {
@@ -25,22 +21,19 @@ import type { SchemaThunkAction } from './store';
2521
import { UUID } from 'bson';
2622
import { isAction } from '../utils';
2723

28-
const ERROR_WARNING = 'An error occurred during schema analysis';
29-
const COMPLEXITY_ABORT_MESSAGE = `
30-
Analysis was aborted due to: Fields count above 1000.
31-
Consider breaking up your data into more collections with smaller documents, and using references to consolidate the data you need.
32-
`;
33-
const INCREASE_MAX_TIME_MS_HINT_MESSAGE =
34-
'Operation exceeded time limit. Please try increasing the maxTimeMS for the query in the filter options.';
35-
3624
const DEFAULT_SAMPLE_SIZE = 1000;
3725

3826
const ERROR_CODE_MAX_TIME_MS_EXPIRED = 50;
3927

28+
export type SchemaAnalysisError = {
29+
errorMessage: string;
30+
errorType: 'TIMEOUT' | 'HIGH_COMPLEXITY' | 'GENERAL';
31+
};
32+
4033
export type SchemaAnalysisState = {
4134
analysisState: AnalysisState;
4235
analysisStartTime?: number;
43-
errorMessage: string;
36+
error?: SchemaAnalysisError;
4437
schema: Schema | null;
4538
resultId: string;
4639
};
@@ -69,7 +62,7 @@ export type AnalysisFailedAction = {
6962
export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
7063
state = getInitialState(),
7164
action
72-
) => {
65+
): SchemaAnalysisState => {
7366
if (
7467
isAction<AnalysisStartedAction>(
7568
action,
@@ -80,7 +73,7 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
8073
...state,
8174
analysisStartTime: action.analysisStartTime,
8275
analysisState: ANALYSIS_STATE_ANALYZING,
83-
errorMessage: '',
76+
error: undefined,
8477
schema: null,
8578
};
8679
}
@@ -101,34 +94,38 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
10194
};
10295
}
10396

97+
if (
98+
isAction<AnalysisFailedAction>(action, SchemaAnalysisActions.analysisFailed)
99+
) {
100+
return {
101+
...state,
102+
error: getErrorDetails(action.error),
103+
analysisState: ANALYSIS_STATE_INITIAL,
104+
resultId: resultId(),
105+
};
106+
}
107+
104108
return state;
105109
};
106110

107-
function getErrorDetails(
108-
errorMessage: string,
109-
errorCode?: number
110-
): Partial<ToastProperties> {
111+
function getErrorDetails(error: Error): SchemaAnalysisError {
112+
const errorCode = (error as MongoError).code;
113+
const errorMessage = error.message || 'Unknown error';
114+
let errorType: SchemaAnalysisError['errorType'] = 'GENERAL';
111115
if (errorCode === ERROR_CODE_MAX_TIME_MS_EXPIRED) {
112-
return { description: INCREASE_MAX_TIME_MS_HINT_MESSAGE };
113-
} else if (errorMessage.includes('Schema analysis aborted: Fields count')) {
114-
return {
115-
description: COMPLEXITY_ABORT_MESSAGE,
116-
actionElement: `<a href="https://www.mongodb.com/cloud/atlas/lp/search-1">Learn more</a>`,
117-
};
118-
} else {
119-
return { description: `${ERROR_WARNING}: ${errorMessage}` };
116+
errorType = 'TIMEOUT';
117+
} else if (error.message.includes('Schema analysis aborted: Fields count')) {
118+
errorType = 'HIGH_COMPLEXITY';
119+
// return {
120+
// description: COMPLEXITY_ABORT_MESSAGE,
121+
// actionElement: `<a href="https://www.mongodb.com/cloud/atlas/lp/search-1">Learn more</a>`,
122+
// };
120123
}
121-
}
122-
123-
function handleError(err: Error & { code?: number }) {
124-
const errorMessage = (err && err.message) || 'Unknown error';
125-
const errorCode = err && err.code;
126124

127-
openToast('schema-analysis-error', {
128-
variant: 'important',
129-
title: 'Schema Analysis Failed',
130-
...getErrorDetails(errorMessage, errorCode),
131-
});
125+
return {
126+
errorType,
127+
errorMessage,
128+
};
132129
}
133130

134131
function resultId(): string {
@@ -137,7 +134,6 @@ function resultId(): string {
137134

138135
const getInitialState = (): SchemaAnalysisState => ({
139136
analysisState: ANALYSIS_STATE_INITIAL,
140-
errorMessage: '',
141137
schema: null,
142138
resultId: resultId(),
143139
});
@@ -357,8 +353,10 @@ export const startAnalysis = (): SchemaThunkAction<
357353
error: err.stack,
358354
}
359355
);
360-
dispatch({ type: SchemaAnalysisActions.analysisFinished, schema: null });
361-
handleError(err as Error);
356+
dispatch({
357+
type: SchemaAnalysisActions.analysisFailed,
358+
error: err as Error,
359+
});
362360
} finally {
363361
analysisAbortControllerRef.current = undefined;
364362
}

0 commit comments

Comments
 (0)