Skip to content

Commit

Permalink
implement custom error to show more accurate error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Hweinstock committed Nov 15, 2024
1 parent 2f092e1 commit 823a0e3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
13 changes: 4 additions & 9 deletions packages/core/src/awsService/ec2/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { DefaultIamClient } from '../../shared/clients/iamClient'
import { ErrorInformation } from '../../shared/errors'
import {
sshAgentSocketVariable,
SSHError,
startSshAgent,
startVscodeRemote,
testSshConnection,
Expand Down Expand Up @@ -154,13 +155,6 @@ export class Ec2Connecter implements vscode.Disposable {
}
}

public throwGeneralConnectionError(selection: Ec2Selection, error: Error) {
this.throwConnectionError('Unable to connect to target instance. ', selection, {
code: 'EC2SSMConnect',
cause: error,
})
}

public async checkForStartSessionError(selection: Ec2Selection): Promise<void> {
await this.checkForInstanceStatusError(selection)

Expand Down Expand Up @@ -189,7 +183,7 @@ export class Ec2Connecter implements vscode.Disposable {
const response = await this.ssmClient.startSession(selection.instanceId)
await this.openSessionInTerminal(response, selection)
} catch (err: unknown) {
this.throwGeneralConnectionError(selection, err as Error)
this.throwConnectionError('', selection, err as Error)
}
}

Expand All @@ -209,7 +203,8 @@ export class Ec2Connecter implements vscode.Disposable {
)
await startVscodeRemote(remoteEnv.SessionProcess, remoteEnv.hostname, '/', remoteEnv.vscPath, remoteUser)
} catch (err) {
this.throwGeneralConnectionError(selection, err as Error)
const message = err instanceof SSHError ? 'Testing SSH connection to instance failed' : ''
this.throwConnectionError(message, selection, err as Error)
} finally {
await this.ssmClient.terminateSession(testSession)
}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/shared/extensions/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import { ArrayConstructor, NonNullObject } from '../utilities/typeConstructors'
import { Settings } from '../settings'
import { VSCODE_EXTENSION_ID } from '../extensions'
import { SSM } from 'aws-sdk'
import { ToolkitError } from '../errors'

const localize = nls.loadMessageBundle()

export const sshAgentSocketVariable = 'SSH_AUTH_SOCK'

export class SSHError extends ToolkitError {}

export function getSshConfigPath(): string {
const sshConfigDir = path.join(fs.getUserHomeDir(), '.ssh')
return path.join(sshConfigDir, 'config')
Expand Down Expand Up @@ -136,7 +139,7 @@ export async function testSshConnection(
})
} catch (error) {
getLogger().error('SSH connection test failed: %O', error)
throw error
throw new SSHError('SSH connection test failed', { cause: error as Error })
}
}

Expand Down

0 comments on commit 823a0e3

Please sign in to comment.