Skip to content

Commit cfe7f9c

Browse files
committed
more linting + error handling
1 parent 9ee4bda commit cfe7f9c

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

packages/compass-indexes/src/components/create-index-form/query-flow-section.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ const QueryFlowSection = ({
153153
// Validate query upon typing
154154
try {
155155
parseFilter(inputQuery);
156+
157+
if (!inputQuery.startsWith('{') || !inputQuery.endsWith('}')) {
158+
isShowSuggestionsButtonDisabled = true;
159+
}
156160
} catch (e) {
157161
isShowSuggestionsButtonDisabled = true;
158162
}

packages/compass-indexes/src/modules/create-index.tsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,19 @@ export const fetchIndexSuggestions = ({
412412
}
413413
}
414414

415+
const throwError = (e?: unknown) => {
416+
dispatch({
417+
type: ActionTypes.SuggestedIndexesFetched,
418+
sampleDocs: sampleDocuments || [],
419+
indexSuggestions: null,
420+
error:
421+
e instanceof Error
422+
? 'Error parsing query. Please follow query structure. ' + e.message
423+
: 'Error parsing query. Please follow query structure.',
424+
indexSuggestionsState: 'error',
425+
});
426+
};
427+
415428
// Analyze namespace and fetch suggestions
416429
try {
417430
const analyzedNamespace = mql.analyzeNamespace(
@@ -426,6 +439,14 @@ export const fetchIndexSuggestions = ({
426439
const results = await mql.suggestIndex([query]);
427440
const indexSuggestions = results?.index;
428441

442+
if (
443+
!indexSuggestions ||
444+
Object.keys(indexSuggestions as Record<string, unknown>).length === 0
445+
) {
446+
throwError();
447+
return;
448+
}
449+
429450
dispatch({
430451
type: ActionTypes.SuggestedIndexesFetched,
431452
sampleDocs: sampleDocuments,
@@ -434,18 +455,8 @@ export const fetchIndexSuggestions = ({
434455
indexSuggestionsState: 'success',
435456
});
436457
} catch (e: unknown) {
437-
dispatch({
438-
type: ActionTypes.SuggestedIndexesFetched,
439-
sampleDocs: sampleDocuments,
440-
indexSuggestions: null,
441-
error:
442-
e instanceof Error
443-
? 'Error parsing query. Please follow query structure. ' + e.message
444-
: 'Error parsing query. Please follow query structure.',
445-
indexSuggestionsState: 'error',
446-
});
447-
448458
track('Error parsing query', { context: 'Create Index Modal' });
459+
throwError(e);
449460
}
450461
};
451462
};
@@ -509,7 +520,7 @@ export const createIndexFormSubmitted = (): IndexesThunkAction<
509520

510521
const formIndexOptions = getState().createIndex.options;
511522

512-
let spec: Record<string, IndexDirection>;
523+
let spec: Record<string, IndexDirection> = {};
513524

514525
try {
515526
if (isQueryFlow) {

0 commit comments

Comments
 (0)