Skip to content

Commit 823a0e3

Browse files
committed
implement custom error to show more accurate error message
1 parent 2f092e1 commit 823a0e3

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

packages/core/src/awsService/ec2/model.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { DefaultIamClient } from '../../shared/clients/iamClient'
2323
import { ErrorInformation } from '../../shared/errors'
2424
import {
2525
sshAgentSocketVariable,
26+
SSHError,
2627
startSshAgent,
2728
startVscodeRemote,
2829
testSshConnection,
@@ -154,13 +155,6 @@ export class Ec2Connecter implements vscode.Disposable {
154155
}
155156
}
156157

157-
public throwGeneralConnectionError(selection: Ec2Selection, error: Error) {
158-
this.throwConnectionError('Unable to connect to target instance. ', selection, {
159-
code: 'EC2SSMConnect',
160-
cause: error,
161-
})
162-
}
163-
164158
public async checkForStartSessionError(selection: Ec2Selection): Promise<void> {
165159
await this.checkForInstanceStatusError(selection)
166160

@@ -189,7 +183,7 @@ export class Ec2Connecter implements vscode.Disposable {
189183
const response = await this.ssmClient.startSession(selection.instanceId)
190184
await this.openSessionInTerminal(response, selection)
191185
} catch (err: unknown) {
192-
this.throwGeneralConnectionError(selection, err as Error)
186+
this.throwConnectionError('', selection, err as Error)
193187
}
194188
}
195189

@@ -209,7 +203,8 @@ export class Ec2Connecter implements vscode.Disposable {
209203
)
210204
await startVscodeRemote(remoteEnv.SessionProcess, remoteEnv.hostname, '/', remoteEnv.vscPath, remoteUser)
211205
} catch (err) {
212-
this.throwGeneralConnectionError(selection, err as Error)
206+
const message = err instanceof SSHError ? 'Testing SSH connection to instance failed' : ''
207+
this.throwConnectionError(message, selection, err as Error)
213208
} finally {
214209
await this.ssmClient.terminateSession(testSession)
215210
}

packages/core/src/shared/extensions/ssh.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import { ArrayConstructor, NonNullObject } from '../utilities/typeConstructors'
1313
import { Settings } from '../settings'
1414
import { VSCODE_EXTENSION_ID } from '../extensions'
1515
import { SSM } from 'aws-sdk'
16+
import { ToolkitError } from '../errors'
1617

1718
const localize = nls.loadMessageBundle()
1819

1920
export const sshAgentSocketVariable = 'SSH_AUTH_SOCK'
2021

22+
export class SSHError extends ToolkitError {}
23+
2124
export function getSshConfigPath(): string {
2225
const sshConfigDir = path.join(fs.getUserHomeDir(), '.ssh')
2326
return path.join(sshConfigDir, 'config')
@@ -136,7 +139,7 @@ export async function testSshConnection(
136139
})
137140
} catch (error) {
138141
getLogger().error('SSH connection test failed: %O', error)
139-
throw error
142+
throw new SSHError('SSH connection test failed', { cause: error as Error })
140143
}
141144
}
142145

0 commit comments

Comments
 (0)