9
9
extractSecrets ,
10
10
mergeSecrets
11
11
} from 'mongodb-data-service' ;
12
- import ConnectionModel from 'mongodb-connection-model' ;
13
12
import ConnectionString from 'mongodb-connection-string-url' ;
14
13
import { EventEmitter } from 'events' ;
15
14
import type { MongoClientOptions } from 'mongodb' ;
@@ -18,6 +17,7 @@ import { v4 as uuidv4 } from 'uuid';
18
17
import { CONNECTION_STATUS } from './views/webview-app/extension-app-message-constants' ;
19
18
import { createLogger } from './logging' ;
20
19
import { ext } from './extensionConstants' ;
20
+ import formatError from './utils/formatError' ;
21
21
import LegacyConnectionModel from './views/webview-app/connection-model/legacy-connection-model' ;
22
22
import { StorageLocation , ConnectionsFromStorage } from './storage/storageController' ;
23
23
import { StorageController , StorageVariables } from './storage' ;
@@ -45,7 +45,7 @@ export interface StoreConnectionInfo {
45
45
name : string ; // Possibly user given name, not unique.
46
46
storageLocation : StorageLocation ;
47
47
connectionOptions ?: ConnectionOptions ;
48
- connectionModel ?: ConnectionModel ;
48
+ connectionModel ?: LegacyConnectionModel ;
49
49
}
50
50
51
51
export enum NewConnectionType {
@@ -75,7 +75,7 @@ export default class ConnectionController {
75
75
// These connections can be saved on the session (runtime),
76
76
// on the workspace, or globally in vscode.
77
77
_connections : { [ connectionId : string ] : StoreConnectionInfoWithConnectionOptions } = { } ;
78
- _activeDataService : DataService | null = null ;
78
+ _activeDataService : DataService | null = null ;
79
79
_storageController : StorageController ;
80
80
81
81
private readonly _serviceName = 'mdb.vscode.savedConnections' ;
@@ -110,6 +110,10 @@ export default class ConnectionController {
110
110
async _migratePreviouslySavedConnection (
111
111
savedConnectionInfo : StoreConnectionInfo
112
112
) : Promise < StoreConnectionInfoWithConnectionOptions > {
113
+ if ( ! savedConnectionInfo . connectionModel ) {
114
+ throw new Error ( 'The connectionModel object is missing in saved connection info.' ) ;
115
+ }
116
+
113
117
// Transform a raw connection model from storage to an ampersand model.
114
118
const newConnectionInfoWithSecrets = convertConnectionModelToInfo ( savedConnectionInfo . connectionModel ) ;
115
119
@@ -140,8 +144,7 @@ export default class ConnectionController {
140
144
} catch ( error ) {
141
145
// Here we're lenient when loading connections in case their
142
146
// 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 } ` ) ;
145
148
return ;
146
149
}
147
150
}
@@ -181,8 +184,7 @@ export default class ConnectionController {
181
184
} catch ( error ) {
182
185
// Here we're lenient when loading connections in case their
183
186
// 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 } ` ) ;
186
188
return ;
187
189
}
188
190
}
@@ -253,8 +255,8 @@ export default class ConnectionController {
253
255
try {
254
256
// eslint-disable-next-line no-new
255
257
new ConnectionString ( uri ) ;
256
- } catch ( err ) {
257
- return ( err as { message : string } ) . message ;
258
+ } catch ( error ) {
259
+ return formatError ( error ) . message ;
258
260
}
259
261
260
262
return null ;
@@ -268,14 +270,7 @@ export default class ConnectionController {
268
270
return false ;
269
271
}
270
272
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 ) ;
279
274
}
280
275
281
276
// Resolves the new connection id when the connection is successfully added.
@@ -305,7 +300,11 @@ export default class ConnectionController {
305
300
306
301
return connectResult . successfullyConnected ;
307
302
} 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 ;
309
308
}
310
309
}
311
310
@@ -378,6 +377,7 @@ export default class ConnectionController {
378
377
} ;
379
378
380
379
log . info ( `Connect called to connect to instance: ${ savedConnectionInfo . name } ` ) ;
380
+
381
381
return this . _connect ( savedConnectionInfo . id , connectionType ) ;
382
382
}
383
383
@@ -414,7 +414,9 @@ export default class ConnectionController {
414
414
connectError = error ;
415
415
}
416
416
417
- if ( this . _endPrevConnectAttempt ( { connectionId, connectingAttemptVersion, newDataService } ) ) {
417
+ const shouldEndPrevConnectAttempt = this . _endPrevConnectAttempt ( { connectionId, connectingAttemptVersion, newDataService } ) ;
418
+
419
+ if ( shouldEndPrevConnectAttempt ) {
418
420
return {
419
421
successfullyConnected : false ,
420
422
connectionErrorMessage : 'connection attempt overriden'
@@ -425,10 +427,9 @@ export default class ConnectionController {
425
427
426
428
if ( connectError ) {
427
429
this . _connecting = false ;
428
- log . info ( 'Failed to connect' ) ;
429
430
this . eventEmitter . emit ( DataServiceEventTypes . CONNECTIONS_DID_CHANGE ) ;
430
431
431
- throw new Error ( `Failed to connect: ${ connectError . message } ` ) ;
432
+ throw connectError ;
432
433
}
433
434
434
435
log . info ( 'Successfully connected' ) ;
@@ -482,7 +483,11 @@ export default class ConnectionController {
482
483
483
484
return true ;
484
485
} 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 ;
486
491
}
487
492
}
488
493
0 commit comments