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

wrote ssh exception handler #228

Merged
merged 10 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 15 additions & 3 deletions src/main/kotlin/com/vk/admstorm/ssh/SshConnectionService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import git4idea.util.GitUIUtil.code
import net.schmizz.sshj.sftp.SFTPClient
import java.io.IOException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

/**
* Service responsible for connecting via SSH to the development server.
Expand Down Expand Up @@ -144,10 +145,12 @@ class SshConnectionService(private var myProject: Project) : Disposable {

override fun run(indicator: ProgressIndicator) {
try {
mySftpChannel = myConnectionBuilder!!.openSftpChannel(2)
mySftpClient = MySshUtils.getSftpClient(mySftpChannel!!)
SshHandler.handle {
mySftpChannel = myConnectionBuilder!!.openSftpChannel(2)
mySftpClient = MySshUtils.getSftpClient(mySftpChannel!!)

onSuccessful?.run()
onSuccessful?.run()
}
} catch (e: SshException) {
if (!cancelled && e.message == "Cancelled by user") {
LOG.warn("Cancelled by user", e)
Expand Down Expand Up @@ -186,6 +189,15 @@ class SshConnectionService(private var myProject: Project) : Disposable {
.show()

LOG.warn("Failed to connect", e)
} catch (e: TimeoutException) {
AdmNotification("Don't forget to touch the yubikey if it blinks when using the AdmStorm plugin's features")
.withTitle("Yubikey waiting timeout")
.withActions(AdmNotification.Action("Reconnect...") { _, notification ->
notification.expire()
connectWithConnector(connector, onSuccessful)
}).show()

LOG.warn("Yubikey waiting timeout", e)
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/com/vk/admstorm/ssh/SshHandler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.vk.admstorm.ssh

import com.intellij.ssh.SshException

object SshHandler {
inline fun <reified T> handle(call: () -> T): T {
return try {
call()
} catch (e: SshException) {
throw unwrap(e)
}
}

fun unwrap(ex: Exception): Throwable {
var cause = ex.cause
while(cause?.cause != null) {
cause = cause.cause
}
return cause ?: ex
}
}
6 changes: 4 additions & 2 deletions src/main/kotlin/com/vk/admstorm/utils/MySshUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.vk.admstorm.notifications.AdmErrorNotification
import com.vk.admstorm.notifications.AdmNotification
import com.vk.admstorm.notifications.AdmWarningNotification
import com.vk.admstorm.ssh.SshConnectionService
import com.vk.admstorm.ssh.SshHandler
import com.vk.admstorm.ssh.YubikeyHandler
import com.vk.admstorm.utils.MyUtils.executeOnPooledThread
import git4idea.util.GitUIUtil.code
Expand Down Expand Up @@ -85,7 +86,9 @@ object MySshUtils {
}

val process = try {
execSync(builder)
SshHandler.handle {
execSync(builder)
}
} catch (e: SshException) {
handleSshException(project, e)
null
Expand Down Expand Up @@ -144,7 +147,6 @@ object MySshUtils {
}
)
.show()

LOG.warn("Unexpected exception for execSync(builder)", e)
}

Expand Down
Loading