11import type { Schema } from 'mongodb-schema' ;
22import { isInternalFieldPath } from 'hadron-document' ;
3- import {
4- openToast ,
5- type ToastProperties ,
6- } from '@mongodb-js/compass-components' ;
73import type { Action , Reducer } from 'redux' ;
8- import type { AggregateOptions } from 'mongodb' ;
4+ import type { AggregateOptions , MongoError } from 'mongodb' ;
95import type { QueryBarService } from '@mongodb-js/compass-query-bar' ;
106import { type AnalysisState } from '../constants/analysis-states' ;
117import {
@@ -25,22 +21,19 @@ import type { SchemaThunkAction } from './store';
2521import { UUID } from 'bson' ;
2622import { 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-
3624const DEFAULT_SAMPLE_SIZE = 1000 ;
3725
3826const ERROR_CODE_MAX_TIME_MS_EXPIRED = 50 ;
3927
28+ export type SchemaAnalysisError = {
29+ errorMessage : string ;
30+ errorType : 'TIMEOUT' | 'HIGH_COMPLEXITY' | 'GENERAL' ;
31+ } ;
32+
4033export 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 = {
6962export 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
134131function resultId ( ) : string {
@@ -137,7 +134,6 @@ function resultId(): string {
137134
138135const 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