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 AnalysisState } from '../constants/analysis-states' ;
106import {
117 ANALYSIS_STATE_ANALYZING ,
@@ -25,21 +21,18 @@ 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 ;
42- errorMessage : string ;
35+ error ?: SchemaAnalysisError ;
4336 schema : Schema | null ;
4437 resultId : string ;
4538} ;
@@ -67,7 +60,7 @@ export type AnalysisFailedAction = {
6760export 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
131128function resultId ( ) : string {
@@ -134,7 +131,6 @@ function resultId(): string {
134131
135132const 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