Skip to content

Commit 1b60150

Browse files
feat: handle 406 error message COMPASS-7189 (#4819)
* feat: handle 406 error message COMPASS-7189 * refactor: clean up * test: match to error type
1 parent 0b7bdba commit 1b60150

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
lines changed

packages/atlas-service/src/main.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ describe('AtlasServiceMain', function () {
412412
json() {
413413
return Promise.resolve({
414414
errorCode: 'ExampleCode',
415+
error: 500,
415416
detail: 'tortillas',
416417
});
417418
},

packages/atlas-service/src/main.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ipcMain, shell, app } from 'electron';
22
import { URL, URLSearchParams } from 'url';
33
import { createHash } from 'crypto';
44
import type { AuthFlowType, MongoDBOIDCPlugin } from '@mongodb-js/oidc-plugin';
5+
import type { AtlasServiceError } from './renderer';
56
import {
67
createMongoDBOIDCPlugin,
78
hookLoggerToMongoLogWriter as oidcPluginHookLoggerToMongoLogWriter,
@@ -46,8 +47,10 @@ const redirectRequestHandler = oidcServerRequestHandler.bind(null, {
4647
/**
4748
* https://www.mongodb.com/docs/atlas/api/atlas-admin-api-ref/#errors
4849
*/
49-
function isServerError(err: any): err is { errorCode: string; detail: string } {
50-
return Boolean(err.errorCode && err.detail);
50+
function isServerError(
51+
err: any
52+
): err is { error: number; errorCode: string; detail: string } {
53+
return Boolean(err.error && err.errorCode && err.detail);
5154
}
5255

5356
function throwIfNetworkTrafficDisabled() {
@@ -76,9 +79,10 @@ export async function throwIfNotOk(
7679
} catch (err) {
7780
// no-op, use the default status and statusText in the message.
7881
}
82+
7983
const err = new Error(serverErrorMessage);
8084
err.name = serverErrorName;
81-
(err as any).statusCode = res.status;
85+
(err as AtlasServiceError).statusCode = res.status;
8286
throw err;
8387
}
8488

packages/atlas-service/src/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export type {
151151
AtlasUserConfig,
152152
IntrospectInfo,
153153
Token,
154-
AtlasServiceNetworkError,
154+
AtlasServiceError,
155155
AIQuery,
156156
AIAggregation,
157157
} from './util';

packages/atlas-service/src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,4 @@ export function validateAIQueryResponse(
142142
}
143143
}
144144

145-
export type AtlasServiceNetworkError = Error & { statusCode: number };
145+
export type AtlasServiceError = Error & { statusCode: number };

packages/compass-aggregations/src/modules/pipeline-builder/pipeline-ai.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { isAction } from '../../utils/is-action';
1111
import type { PipelineParserError } from './pipeline-parser/utils';
1212
import type Stage from './stage';
1313
import { updatePipelinePreview } from './builder-helpers';
14-
import type { AtlasServiceNetworkError } from '@mongodb-js/atlas-service/renderer';
14+
import type { AtlasServiceError } from '@mongodb-js/atlas-service/renderer';
1515

1616
const { log, mongoLogId, track } = createLoggerAndTelemetry('AI-PIPELINE-UI');
1717

@@ -243,13 +243,13 @@ export const runAIPipelineGeneration = (
243243
}
244244
trackAndLogFailed({
245245
editor_view_type,
246-
errorCode: (err as AtlasServiceNetworkError).statusCode,
247-
errorMessage: (err as AtlasServiceNetworkError).message,
246+
errorCode: (err as AtlasServiceError).statusCode,
247+
errorMessage: (err as AtlasServiceError).message,
248248
errorName: 'request_error',
249249
});
250250
// We're going to reset input state with this error, show the error in the
251251
// toast instead
252-
if ((err as AtlasServiceNetworkError).statusCode === 401) {
252+
if ((err as AtlasServiceError).statusCode === 401) {
253253
openToast('ai-unauthorized', {
254254
variant: 'important',
255255
title: 'Network Error',
@@ -259,8 +259,8 @@ export const runAIPipelineGeneration = (
259259
}
260260
dispatch({
261261
type: AIPipelineActionTypes.AIPipelineFailed,
262-
errorMessage: (err as AtlasServiceNetworkError).message,
263-
networkErrorCode: (err as AtlasServiceNetworkError).statusCode ?? -1,
262+
errorMessage: (err as AtlasServiceError).message,
263+
networkErrorCode: (err as AtlasServiceError).statusCode ?? -1,
264264
});
265265
return;
266266
} finally {
@@ -286,7 +286,7 @@ export const runAIPipelineGeneration = (
286286
} catch (err) {
287287
trackAndLogFailed({
288288
editor_view_type,
289-
errorCode: (err as AtlasServiceNetworkError).statusCode,
289+
errorCode: (err as AtlasServiceError).statusCode,
290290
errorMessage: (err as Error).message,
291291
errorName: 'empty_pipeline_error',
292292
});

packages/compass-query-bar/src/stores/ai-query-reducer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import type { QueryFormFields } from '../constants/query-properties';
1414
import { DEFAULT_FIELD_VALUES } from '../constants/query-bar-store';
1515
import { openToast } from '@mongodb-js/compass-components';
16-
import type { AtlasServiceNetworkError } from '@mongodb-js/atlas-service/renderer';
16+
import type { AtlasServiceError } from '@mongodb-js/atlas-service/renderer';
1717

1818
const { log, mongoLogId, track } = createLoggerAndTelemetry('AI-QUERY-UI');
1919

@@ -204,12 +204,12 @@ export const runAIQuery = (
204204
}
205205
trackAndLogFailed({
206206
errorName: 'request_error',
207-
errorCode: (err as AtlasServiceNetworkError).statusCode,
208-
errorMessage: (err as AtlasServiceNetworkError).message,
207+
errorCode: (err as AtlasServiceError).statusCode,
208+
errorMessage: (err as AtlasServiceError).message,
209209
});
210210
// We're going to reset input state with this error, show the error in the
211211
// toast instead
212-
if ((err as AtlasServiceNetworkError).statusCode === 401) {
212+
if ((err as AtlasServiceError).statusCode === 401) {
213213
openToast('ai-unauthorized', {
214214
variant: 'important',
215215
title: 'Network Error',
@@ -219,8 +219,8 @@ export const runAIQuery = (
219219
}
220220
dispatch({
221221
type: AIQueryActionTypes.AIQueryFailed,
222-
errorMessage: (err as AtlasServiceNetworkError).message,
223-
networkErrorCode: (err as AtlasServiceNetworkError).statusCode ?? -1,
222+
errorMessage: (err as AtlasServiceError).message,
223+
networkErrorCode: (err as AtlasServiceError).statusCode ?? -1,
224224
});
225225
return;
226226
} finally {
@@ -246,7 +246,7 @@ export const runAIQuery = (
246246
} catch (err: any) {
247247
trackAndLogFailed({
248248
errorName: 'could_not_parse_fields',
249-
errorCode: (err as AtlasServiceNetworkError).statusCode,
249+
errorCode: (err as AtlasServiceError).statusCode,
250250
errorMessage: err?.message,
251251
});
252252
dispatch({

0 commit comments

Comments
 (0)