Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(connect): use extracted connection logic from mongosh MONGOSH-1080 #390

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
651 changes: 392 additions & 259 deletions package-lock.json

Large diffs are not rendered by default.

23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -897,12 +897,12 @@
}
},
"dependencies": {
"@babel/parser": "^7.16.12",
"@babel/traverse": "^7.16.10",
"@babel/parser": "^7.17.0",
"@babel/traverse": "^7.17.0",
"@fortawesome/fontawesome-svg-core": "^1.2.36",
"@fortawesome/free-solid-svg-icons": "^5.15.4",
"@fortawesome/react-fontawesome": "^0.1.16",
"@iconify-icons/codicon": "^1.1.29",
"@fortawesome/react-fontawesome": "^0.1.17",
"@iconify-icons/codicon": "^1.1.30",
"@iconify/react": "^1.1.4",
"@leafygreen-ui/logo": "^6.1.0",
"@leafygreen-ui/toggle": "^7.0.5",
Expand All @@ -912,16 +912,15 @@
"@mongosh/shell-api": "^1.1.9",
"analytics-node": "^5.1.2",
"bson": "^4.6.1",
"bson-transpilers": "^1.4.0",
"bson-transpilers": "^1.5.0",
"classnames": "^2.3.1",
"debug": "^4.3.3",
"dotenv": "^8.6.0",
"micromatch": "^4.0.4",
"mongodb": "^4.3.1",
"mongodb-cloud-info": "^1.1.3",
"mongodb-connection-model": "^21.11.1",
"mongodb-connection-string-url": "^2.4.1",
"mongodb-data-service": "^21.15.1",
"mongodb-connection-string-url": "^2.4.2",
"mongodb-data-service": "^21.16.1",
"mongodb-ns": "^2.3.0",
"mongodb-schema": "^9.0.0",
"numeral": "^2.0.6",
Expand All @@ -933,7 +932,7 @@
"uuid": "^8.3.2",
"vscode-languageclient": "^7.0.0",
"vscode-languageserver": "^7.0.0",
"vscode-languageserver-textdocument": "^1.0.3"
"vscode-languageserver-textdocument": "^1.0.4"
},
"devDependencies": {
"@types/analytics-node": "^3.1.7",
Expand All @@ -947,16 +946,16 @@
"@types/glob": "^7.2.0",
"@types/jest": "^26.0.24",
"@types/mocha": "^8.2.3",
"@types/node": "^14.18.9",
"@types/react": "^17.0.38",
"@types/node": "^14.18.10",
"@types/react": "^17.0.39",
"@types/react-dom": "^17.0.11",
"@types/sinon": "^9.0.11",
"@types/uuid": "^8.3.4",
"@types/vscode": "^1.58.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"autoprefixer": "^9.8.8",
"chai": "^4.3.5",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"chai-fs": "^2.0.0",
"chai-json-schema": "^1.5.1",
Expand Down
5 changes: 3 additions & 2 deletions scripts/update-grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import ora = require('ora');
import download = require('download');
import meow = require('meow');

import formatError from '../src/utils/formatError';

const DEFAULT_DEST = path.join(__dirname, '..', 'syntaxes');

const languageURL =
Expand Down Expand Up @@ -52,7 +54,6 @@ const cli = meow(
`Downloaded to ${path.join(cli.flags.dest as string, 'mongodb.tmLanguage.json')}`
);
} catch (error) {
const printableError = error as { message: string };
ui.fail(`Download failed: ${printableError.message}`);
ui.fail(`Download failed: ${formatError(error).message}`);
}
})();
49 changes: 27 additions & 22 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
extractSecrets,
mergeSecrets
} from 'mongodb-data-service';
import ConnectionModel from 'mongodb-connection-model';
import ConnectionString from 'mongodb-connection-string-url';
import { EventEmitter } from 'events';
import type { MongoClientOptions } from 'mongodb';
Expand All @@ -18,6 +17,7 @@ import { v4 as uuidv4 } from 'uuid';
import { CONNECTION_STATUS } from './views/webview-app/extension-app-message-constants';
import { createLogger } from './logging';
import { ext } from './extensionConstants';
import formatError from './utils/formatError';
import LegacyConnectionModel from './views/webview-app/connection-model/legacy-connection-model';
import { StorageLocation, ConnectionsFromStorage } from './storage/storageController';
import { StorageController, StorageVariables } from './storage';
Expand Down Expand Up @@ -45,7 +45,7 @@ export interface StoreConnectionInfo {
name: string; // Possibly user given name, not unique.
storageLocation: StorageLocation;
connectionOptions?: ConnectionOptions;
connectionModel?: ConnectionModel;
connectionModel?: LegacyConnectionModel;
}

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

private readonly _serviceName = 'mdb.vscode.savedConnections';
Expand Down Expand Up @@ -110,6 +110,10 @@ export default class ConnectionController {
async _migratePreviouslySavedConnection(
savedConnectionInfo: StoreConnectionInfo
): Promise<StoreConnectionInfoWithConnectionOptions> {
if (!savedConnectionInfo.connectionModel) {
throw new Error('The connectionModel object is missing in saved connection info.');
}

// Transform a raw connection model from storage to an ampersand model.
const newConnectionInfoWithSecrets = convertConnectionModelToInfo(savedConnectionInfo.connectionModel);

Expand Down Expand Up @@ -140,8 +144,7 @@ export default class ConnectionController {
} catch (error) {
// Here we're lenient when loading connections in case their
// connections have become corrupted.
const printableError = error as { message: string };
log.error(`Connection migration failed: ${printableError.message}`);
log.error(`Connection migration failed: ${formatError(error).message}`);
return;
}
}
Expand Down Expand Up @@ -181,8 +184,7 @@ export default class ConnectionController {
} catch (error) {
// Here we're lenient when loading connections in case their
// connections have become corrupted.
const printableError = error as { message: string };
log.error(`Merging connection with secrets failed: ${printableError.message}`);
log.error(`Merging connection with secrets failed: ${formatError(error).message}`);
return;
}
}
Expand Down Expand Up @@ -253,8 +255,8 @@ export default class ConnectionController {
try {
// eslint-disable-next-line no-new
new ConnectionString(uri);
} catch (err) {
return (err as { message: string }).message;
} catch (error) {
return formatError(error).message;
}

return null;
Expand All @@ -268,14 +270,7 @@ export default class ConnectionController {
return false;
}

try {
return this.addNewConnectionStringAndConnect(connectionString);
} catch (error) {
const printableError = error as { message: string };
void vscode.window.showErrorMessage(printableError.message);

return false;
}
return this.addNewConnectionStringAndConnect(connectionString);
}

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

return connectResult.successfullyConnected;
} catch (error) {
throw new Error(`Unable to create connection: ${error}`);
const printableError = formatError(error);
log.error('Failed to connect', printableError);
void vscode.window.showErrorMessage(`Unable to connect: ${printableError.message}`);

return false;
}
}

Expand Down Expand Up @@ -378,6 +377,7 @@ export default class ConnectionController {
};

log.info(`Connect called to connect to instance: ${savedConnectionInfo.name}`);

return this._connect(savedConnectionInfo.id, connectionType);
}

Expand Down Expand Up @@ -414,7 +414,9 @@ export default class ConnectionController {
connectError = error;
}

if (this._endPrevConnectAttempt({ connectionId, connectingAttemptVersion, newDataService })) {
const shouldEndPrevConnectAttempt = this._endPrevConnectAttempt({ connectionId, connectingAttemptVersion, newDataService });

if (shouldEndPrevConnectAttempt) {
return {
successfullyConnected: false,
connectionErrorMessage: 'connection attempt overriden'
Expand All @@ -425,10 +427,9 @@ export default class ConnectionController {

if (connectError) {
this._connecting = false;
log.info('Failed to connect');
this.eventEmitter.emit(DataServiceEventTypes.CONNECTIONS_DID_CHANGE);

throw new Error(`Failed to connect: ${connectError.message}`);
throw connectError;
}

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

return true;
} catch (error) {
throw new Error(`Unable to connect: ${error}`);
const printableError = formatError(error);
log.error('Failed to connect', printableError);
void vscode.window.showErrorMessage(`Unable to connect: ${printableError.message}`);

return false;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/editors/collectionDocumentsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { URLSearchParams } from 'url';
import CollectionDocumentsOperationsStore from './collectionDocumentsOperationsStore';
import ConnectionController from '../connectionController';
import EditDocumentCodeLensProvider from './editDocumentCodeLensProvider';
import formatError from '../utils/formatError';
import { StatusView } from '../views';

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

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

void vscode.window.showErrorMessage(errorMessage);

Expand Down
13 changes: 4 additions & 9 deletions src/editors/editorsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import DocumentIdStore from './documentIdStore';
import { DocumentSource } from '../documentSource';
import EditDocumentCodeLensProvider from './editDocumentCodeLensProvider';
import type { EditDocumentInfo } from '../types/editDocumentInfoType';
import formatError from '../utils/formatError';
import { MemoryFileSystemProvider } from './memoryFileSystemProvider';
import MongoDBDocumentService, {
DOCUMENT_ID_URI_IDENTIFIER,
Expand Down Expand Up @@ -195,9 +196,7 @@ export default class EditorsController {

return true;
} catch (error) {
const printableError = error as { message: string };

void vscode.window.showErrorMessage(printableError.message);
void vscode.window.showErrorMessage(formatError(error).message);

return false;
}
Expand Down Expand Up @@ -253,9 +252,7 @@ export default class EditorsController {

return true;
} catch (error) {
const printableError = error as { message: string };

void vscode.window.showErrorMessage(printableError.message);
void vscode.window.showErrorMessage(formatError(error).message);

return false;
}
Expand All @@ -280,10 +277,8 @@ export default class EditorsController {

return true;
} catch (error) {
const printableError = error as { message: string };

void vscode.window.showErrorMessage(
`Unable to open documents: ${printableError.message}`
`Unable to open documents: ${formatError(error).message}`
);

return false;
Expand Down
9 changes: 3 additions & 6 deletions src/editors/mongoDBDocumentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createLogger } from '../logging';
import DocumentIdStore from './documentIdStore';
import { DocumentSource } from '../documentSource';
import type { EditDocumentInfo } from '../types/editDocumentInfoType';
import formatError from '../utils/formatError';
import { StatusView } from '../views';
import TelemetryService from '../telemetry/telemetryService';

Expand Down Expand Up @@ -100,11 +101,9 @@ export default class MongoDBDocumentService {
this._statusView.hideMessage();
this._telemetryService.trackDocumentUpdated(source, true);
} catch (error) {
const printableError = error as { message: string };

this._statusView.hideMessage();

return this._saveDocumentFailed(printableError.message);
return this._saveDocumentFailed(formatError(error).message);
}
}

Expand Down Expand Up @@ -153,11 +152,9 @@ export default class MongoDBDocumentService {
EJSON.stringify(documents[0])
) as EJSON.SerializableTypes;
} catch (error) {
const printableError = error as { message: string };

this._statusView.hideMessage();

return this._fetchDocumentFailed(printableError.message);
return this._fetchDocumentFailed(formatError(error).message);
}
}
}
Loading