Skip to content

Commit 688c672

Browse files
committed
use error banner instead of toast
1 parent bf55762 commit 688c672

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 AnalysisState } from '../constants/analysis-states';
106
import {
117
ANALYSIS_STATE_ANALYZING,
@@ -25,21 +21,18 @@ 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;
42-
errorMessage: string;
35+
error?: SchemaAnalysisError;
4336
schema: Schema | null;
4437
resultId: string;
4538
};
@@ -67,7 +60,7 @@ export type AnalysisFailedAction = {
6760
export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
6861
state = getInitialState(),
6962
action
70-
) => {
63+
): SchemaAnalysisState => {
7164
if (
7265
isAction<AnalysisStartedAction>(
7366
action,
@@ -77,7 +70,7 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
7770
return {
7871
...state,
7972
analysisState: ANALYSIS_STATE_ANALYZING,
80-
errorMessage: '',
73+
error: undefined,
8174
schema: null,
8275
};
8376
}
@@ -98,34 +91,38 @@ export const schemaAnalysisReducer: Reducer<SchemaAnalysisState, Action> = (
9891
};
9992
}
10093

94+
if (
95+
isAction<AnalysisFailedAction>(action, SchemaAnalysisActions.analysisFailed)
96+
) {
97+
return {
98+
...state,
99+
error: getErrorDetails(action.error),
100+
analysisState: ANALYSIS_STATE_INITIAL,
101+
resultId: resultId(),
102+
};
103+
}
104+
101105
return state;
102106
};
103107

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

124-
openToast('schema-analysis-error', {
125-
variant: 'important',
126-
title: 'Schema Analysis Failed',
127-
...getErrorDetails(errorMessage, errorCode),
128-
});
122+
return {
123+
errorType,
124+
errorMessage,
125+
};
129126
}
130127

131128
function resultId(): string {
@@ -134,7 +131,6 @@ function resultId(): string {
134131

135132
const getInitialState = (): SchemaAnalysisState => ({
136133
analysisState: ANALYSIS_STATE_INITIAL,
137-
errorMessage: '',
138134
schema: null,
139135
resultId: resultId(),
140136
});
@@ -284,8 +280,10 @@ export const startAnalysis = (): SchemaThunkAction<
284280
error: err.stack,
285281
}
286282
);
287-
dispatch({ type: SchemaAnalysisActions.analysisFinished, schema: null });
288-
handleError(err as Error);
283+
dispatch({
284+
type: SchemaAnalysisActions.analysisFailed,
285+
error: err as Error,
286+
});
289287
} finally {
290288
analysisAbortControllerRef.current = undefined;
291289
}

0 commit comments

Comments
 (0)