Skip to content

Commit cbab069

Browse files
feat(connect): use extracted connection logic from mongosh MONGOSH-1080 (#390)
* feat: export-to-language-connect-and-telemetry VSCODE-318, VSCODE-322 * refactor: set all addons * refactor: get rid of the connection model dependency * build: update package lock * build: return connection model because it is a peer dependency * refactor: keep original error stack trace * refactor: remove comma * test: get mongo client options from connection controller * refactor: remove connection model from dependencies * test: increase timeout for export to language * tests: update mocks for getMongoClientConnectionOptions * refactor: check for a built-in error object
1 parent 3e5424c commit cbab069

29 files changed

+579
-474
lines changed

package-lock.json

Lines changed: 392 additions & 259 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,12 @@
897897
}
898898
},
899899
"dependencies": {
900-
"@babel/parser": "^7.16.12",
901-
"@babel/traverse": "^7.16.10",
900+
"@babel/parser": "^7.17.0",
901+
"@babel/traverse": "^7.17.0",
902902
"@fortawesome/fontawesome-svg-core": "^1.2.36",
903903
"@fortawesome/free-solid-svg-icons": "^5.15.4",
904-
"@fortawesome/react-fontawesome": "^0.1.16",
905-
"@iconify-icons/codicon": "^1.1.29",
904+
"@fortawesome/react-fontawesome": "^0.1.17",
905+
"@iconify-icons/codicon": "^1.1.30",
906906
"@iconify/react": "^1.1.4",
907907
"@leafygreen-ui/logo": "^6.1.0",
908908
"@leafygreen-ui/toggle": "^7.0.5",
@@ -912,16 +912,15 @@
912912
"@mongosh/shell-api": "^1.1.9",
913913
"analytics-node": "^5.1.2",
914914
"bson": "^4.6.1",
915-
"bson-transpilers": "^1.4.0",
915+
"bson-transpilers": "^1.5.0",
916916
"classnames": "^2.3.1",
917917
"debug": "^4.3.3",
918918
"dotenv": "^8.6.0",
919919
"micromatch": "^4.0.4",
920920
"mongodb": "^4.3.1",
921921
"mongodb-cloud-info": "^1.1.3",
922-
"mongodb-connection-model": "^21.11.1",
923-
"mongodb-connection-string-url": "^2.4.1",
924-
"mongodb-data-service": "^21.15.1",
922+
"mongodb-connection-string-url": "^2.4.2",
923+
"mongodb-data-service": "^21.16.1",
925924
"mongodb-ns": "^2.3.0",
926925
"mongodb-schema": "^9.0.0",
927926
"numeral": "^2.0.6",
@@ -933,7 +932,7 @@
933932
"uuid": "^8.3.2",
934933
"vscode-languageclient": "^7.0.0",
935934
"vscode-languageserver": "^7.0.0",
936-
"vscode-languageserver-textdocument": "^1.0.3"
935+
"vscode-languageserver-textdocument": "^1.0.4"
937936
},
938937
"devDependencies": {
939938
"@types/analytics-node": "^3.1.7",
@@ -947,16 +946,16 @@
947946
"@types/glob": "^7.2.0",
948947
"@types/jest": "^26.0.24",
949948
"@types/mocha": "^8.2.3",
950-
"@types/node": "^14.18.9",
951-
"@types/react": "^17.0.38",
949+
"@types/node": "^14.18.10",
950+
"@types/react": "^17.0.39",
952951
"@types/react-dom": "^17.0.11",
953952
"@types/sinon": "^9.0.11",
954953
"@types/uuid": "^8.3.4",
955954
"@types/vscode": "^1.58.1",
956955
"@typescript-eslint/eslint-plugin": "^4.33.0",
957956
"@typescript-eslint/parser": "^4.33.0",
958957
"autoprefixer": "^9.8.8",
959-
"chai": "^4.3.5",
958+
"chai": "^4.3.6",
960959
"chai-as-promised": "^7.1.1",
961960
"chai-fs": "^2.0.0",
962961
"chai-json-schema": "^1.5.1",

scripts/update-grammar.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import ora = require('ora');
66
import download = require('download');
77
import meow = require('meow');
88

9+
import formatError from '../src/utils/formatError';
10+
911
const DEFAULT_DEST = path.join(__dirname, '..', 'syntaxes');
1012

1113
const languageURL =
@@ -52,7 +54,6 @@ const cli = meow(
5254
`Downloaded to ${path.join(cli.flags.dest as string, 'mongodb.tmLanguage.json')}`
5355
);
5456
} catch (error) {
55-
const printableError = error as { message: string };
56-
ui.fail(`Download failed: ${printableError.message}`);
57+
ui.fail(`Download failed: ${formatError(error).message}`);
5758
}
5859
})();

src/connectionController.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
extractSecrets,
1010
mergeSecrets
1111
} from 'mongodb-data-service';
12-
import ConnectionModel from 'mongodb-connection-model';
1312
import ConnectionString from 'mongodb-connection-string-url';
1413
import { EventEmitter } from 'events';
1514
import type { MongoClientOptions } from 'mongodb';
@@ -18,6 +17,7 @@ import { v4 as uuidv4 } from 'uuid';
1817
import { CONNECTION_STATUS } from './views/webview-app/extension-app-message-constants';
1918
import { createLogger } from './logging';
2019
import { ext } from './extensionConstants';
20+
import formatError from './utils/formatError';
2121
import LegacyConnectionModel from './views/webview-app/connection-model/legacy-connection-model';
2222
import { StorageLocation, ConnectionsFromStorage } from './storage/storageController';
2323
import { StorageController, StorageVariables } from './storage';
@@ -45,7 +45,7 @@ export interface StoreConnectionInfo {
4545
name: string; // Possibly user given name, not unique.
4646
storageLocation: StorageLocation;
4747
connectionOptions?: ConnectionOptions;
48-
connectionModel?: ConnectionModel;
48+
connectionModel?: LegacyConnectionModel;
4949
}
5050

5151
export enum NewConnectionType {
@@ -75,7 +75,7 @@ export default class ConnectionController {
7575
// These connections can be saved on the session (runtime),
7676
// on the workspace, or globally in vscode.
7777
_connections: { [connectionId: string]: StoreConnectionInfoWithConnectionOptions } = {};
78-
_activeDataService: DataService| null = null;
78+
_activeDataService: DataService | null = null;
7979
_storageController: StorageController;
8080

8181
private readonly _serviceName = 'mdb.vscode.savedConnections';
@@ -110,6 +110,10 @@ export default class ConnectionController {
110110
async _migratePreviouslySavedConnection(
111111
savedConnectionInfo: StoreConnectionInfo
112112
): Promise<StoreConnectionInfoWithConnectionOptions> {
113+
if (!savedConnectionInfo.connectionModel) {
114+
throw new Error('The connectionModel object is missing in saved connection info.');
115+
}
116+
113117
// Transform a raw connection model from storage to an ampersand model.
114118
const newConnectionInfoWithSecrets = convertConnectionModelToInfo(savedConnectionInfo.connectionModel);
115119

@@ -140,8 +144,7 @@ export default class ConnectionController {
140144
} catch (error) {
141145
// Here we're lenient when loading connections in case their
142146
// connections have become corrupted.
143-
const printableError = error as { message: string };
144-
log.error(`Connection migration failed: ${printableError.message}`);
147+
log.error(`Connection migration failed: ${formatError(error).message}`);
145148
return;
146149
}
147150
}
@@ -181,8 +184,7 @@ export default class ConnectionController {
181184
} catch (error) {
182185
// Here we're lenient when loading connections in case their
183186
// connections have become corrupted.
184-
const printableError = error as { message: string };
185-
log.error(`Merging connection with secrets failed: ${printableError.message}`);
187+
log.error(`Merging connection with secrets failed: ${formatError(error).message}`);
186188
return;
187189
}
188190
}
@@ -253,8 +255,8 @@ export default class ConnectionController {
253255
try {
254256
// eslint-disable-next-line no-new
255257
new ConnectionString(uri);
256-
} catch (err) {
257-
return (err as { message: string }).message;
258+
} catch (error) {
259+
return formatError(error).message;
258260
}
259261

260262
return null;
@@ -268,14 +270,7 @@ export default class ConnectionController {
268270
return false;
269271
}
270272

271-
try {
272-
return this.addNewConnectionStringAndConnect(connectionString);
273-
} catch (error) {
274-
const printableError = error as { message: string };
275-
void vscode.window.showErrorMessage(printableError.message);
276-
277-
return false;
278-
}
273+
return this.addNewConnectionStringAndConnect(connectionString);
279274
}
280275

281276
// Resolves the new connection id when the connection is successfully added.
@@ -305,7 +300,11 @@ export default class ConnectionController {
305300

306301
return connectResult.successfullyConnected;
307302
} catch (error) {
308-
throw new Error(`Unable to create connection: ${error}`);
303+
const printableError = formatError(error);
304+
log.error('Failed to connect', printableError);
305+
void vscode.window.showErrorMessage(`Unable to connect: ${printableError.message}`);
306+
307+
return false;
309308
}
310309
}
311310

@@ -378,6 +377,7 @@ export default class ConnectionController {
378377
};
379378

380379
log.info(`Connect called to connect to instance: ${savedConnectionInfo.name}`);
380+
381381
return this._connect(savedConnectionInfo.id, connectionType);
382382
}
383383

@@ -414,7 +414,9 @@ export default class ConnectionController {
414414
connectError = error;
415415
}
416416

417-
if (this._endPrevConnectAttempt({ connectionId, connectingAttemptVersion, newDataService })) {
417+
const shouldEndPrevConnectAttempt = this._endPrevConnectAttempt({ connectionId, connectingAttemptVersion, newDataService });
418+
419+
if (shouldEndPrevConnectAttempt) {
418420
return {
419421
successfullyConnected: false,
420422
connectionErrorMessage: 'connection attempt overriden'
@@ -425,10 +427,9 @@ export default class ConnectionController {
425427

426428
if (connectError) {
427429
this._connecting = false;
428-
log.info('Failed to connect');
429430
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);
430431

431-
throw new Error(`Failed to connect: ${connectError.message}`);
432+
throw connectError;
432433
}
433434

434435
log.info('Successfully connected');
@@ -482,7 +483,11 @@ export default class ConnectionController {
482483

483484
return true;
484485
} catch (error) {
485-
throw new Error(`Unable to connect: ${error}`);
486+
const printableError = formatError(error);
487+
log.error('Failed to connect', printableError);
488+
void vscode.window.showErrorMessage(`Unable to connect: ${printableError.message}`);
489+
490+
return false;
486491
}
487492
}
488493

src/editors/collectionDocumentsProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { URLSearchParams } from 'url';
55
import CollectionDocumentsOperationsStore from './collectionDocumentsOperationsStore';
66
import ConnectionController from '../connectionController';
77
import EditDocumentCodeLensProvider from './editDocumentCodeLensProvider';
8+
import formatError from '../utils/formatError';
89
import { StatusView } from '../views';
910

1011
export const NAMESPACE_URI_IDENTIFIER = 'namespace';
@@ -104,8 +105,7 @@ implements vscode.TextDocumentContentProvider {
104105

105106
return JSON.stringify(documents, null, 2);
106107
} catch (error) {
107-
const printableError = error as { message: string };
108-
const errorMessage = `Unable to list documents: ${printableError.message}`;
108+
const errorMessage = `Unable to list documents: ${formatError(error).message}`;
109109

110110
void vscode.window.showErrorMessage(errorMessage);
111111

src/editors/editorsController.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import DocumentIdStore from './documentIdStore';
1818
import { DocumentSource } from '../documentSource';
1919
import EditDocumentCodeLensProvider from './editDocumentCodeLensProvider';
2020
import type { EditDocumentInfo } from '../types/editDocumentInfoType';
21+
import formatError from '../utils/formatError';
2122
import { MemoryFileSystemProvider } from './memoryFileSystemProvider';
2223
import MongoDBDocumentService, {
2324
DOCUMENT_ID_URI_IDENTIFIER,
@@ -195,9 +196,7 @@ export default class EditorsController {
195196

196197
return true;
197198
} catch (error) {
198-
const printableError = error as { message: string };
199-
200-
void vscode.window.showErrorMessage(printableError.message);
199+
void vscode.window.showErrorMessage(formatError(error).message);
201200

202201
return false;
203202
}
@@ -253,9 +252,7 @@ export default class EditorsController {
253252

254253
return true;
255254
} catch (error) {
256-
const printableError = error as { message: string };
257-
258-
void vscode.window.showErrorMessage(printableError.message);
255+
void vscode.window.showErrorMessage(formatError(error).message);
259256

260257
return false;
261258
}
@@ -280,10 +277,8 @@ export default class EditorsController {
280277

281278
return true;
282279
} catch (error) {
283-
const printableError = error as { message: string };
284-
285280
void vscode.window.showErrorMessage(
286-
`Unable to open documents: ${printableError.message}`
281+
`Unable to open documents: ${formatError(error).message}`
287282
);
288283

289284
return false;

src/editors/mongoDBDocumentService.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createLogger } from '../logging';
77
import DocumentIdStore from './documentIdStore';
88
import { DocumentSource } from '../documentSource';
99
import type { EditDocumentInfo } from '../types/editDocumentInfoType';
10+
import formatError from '../utils/formatError';
1011
import { StatusView } from '../views';
1112
import TelemetryService from '../telemetry/telemetryService';
1213

@@ -100,11 +101,9 @@ export default class MongoDBDocumentService {
100101
this._statusView.hideMessage();
101102
this._telemetryService.trackDocumentUpdated(source, true);
102103
} catch (error) {
103-
const printableError = error as { message: string };
104-
105104
this._statusView.hideMessage();
106105

107-
return this._saveDocumentFailed(printableError.message);
106+
return this._saveDocumentFailed(formatError(error).message);
108107
}
109108
}
110109

@@ -153,11 +152,9 @@ export default class MongoDBDocumentService {
153152
EJSON.stringify(documents[0])
154153
) as EJSON.SerializableTypes;
155154
} catch (error) {
156-
const printableError = error as { message: string };
157-
158155
this._statusView.hideMessage();
159156

160-
return this._fetchDocumentFailed(printableError.message);
157+
return this._fetchDocumentFailed(formatError(error).message);
161158
}
162159
}
163160
}

0 commit comments

Comments
 (0)