Skip to content

Commit 37b8eca

Browse files
committed
added SshHandler, rewrote logic for exceptions, new exception, new notification
1 parent 11f8fdf commit 37b8eca

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

src/main/kotlin/com/vk/admstorm/ssh/SshConnectionService.kt

+15-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import git4idea.util.GitUIUtil.code
2727
import net.schmizz.sshj.sftp.SFTPClient
2828
import java.io.IOException
2929
import java.util.concurrent.TimeUnit
30+
import java.util.concurrent.TimeoutException
3031

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

145146
override fun run(indicator: ProgressIndicator) {
146147
try {
147-
mySftpChannel = myConnectionBuilder!!.openSftpChannel(2)
148-
mySftpClient = MySshUtils.getSftpClient(mySftpChannel!!)
148+
SshHandler.handle {
149+
mySftpChannel = myConnectionBuilder!!.openSftpChannel(2)
150+
mySftpClient = MySshUtils.getSftpClient(mySftpChannel!!)
149151

150-
onSuccessful?.run()
152+
onSuccessful?.run()
153+
}
151154
} catch (e: SshException) {
152155
if (!cancelled && e.message == "Cancelled by user") {
153156
LOG.warn("Cancelled by user", e)
@@ -186,6 +189,15 @@ class SshConnectionService(private var myProject: Project) : Disposable {
186189
.show()
187190

188191
LOG.warn("Failed to connect", e)
192+
} catch (e: TimeoutException) {
193+
AdmNotification("Don't forget to touch the yubikey if it blinks when using the AdmStorm plugin's features")
194+
.withTitle("Yubikey waiting timeout")
195+
.withActions(AdmNotification.Action("Reconnect...") { _, notification ->
196+
notification.expire()
197+
connectWithConnector(connector, onSuccessful)
198+
}).show()
199+
200+
LOG.warn("Yubikey waiting timeout", e)
189201
}
190202
}
191203

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.vk.admstorm.ssh
2+
3+
import com.intellij.ssh.SshException
4+
5+
object SshHandler {
6+
inline fun <reified T> handle(call: () -> T): T {
7+
return try {
8+
call()
9+
} catch (e: SshException) {
10+
throw unwrap(e)
11+
}
12+
}
13+
14+
fun unwrap(ex: Exception): Throwable {
15+
var cause = ex.cause
16+
while(cause?.cause != null) {
17+
cause = cause.cause
18+
}
19+
return cause ?: ex
20+
}
21+
}

src/main/kotlin/com/vk/admstorm/utils/MySshUtils.kt

+10-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.vk.admstorm.notifications.AdmErrorNotification
1616
import com.vk.admstorm.notifications.AdmNotification
1717
import com.vk.admstorm.notifications.AdmWarningNotification
1818
import com.vk.admstorm.ssh.SshConnectionService
19+
import com.vk.admstorm.ssh.SshHandler
1920
import com.vk.admstorm.ssh.YubikeyHandler
2021
import com.vk.admstorm.utils.MyUtils.executeOnPooledThread
2122
import git4idea.util.GitUIUtil.code
@@ -85,20 +86,23 @@ object MySshUtils {
8586
}
8687

8788
val process = try {
88-
execSync(builder)
89+
SshHandler.handle {
90+
execSync(builder)
91+
}
8992
} catch (e: SshException) {
9093
handleSshException(project, e)
9194
null
92-
} catch (e: TimeoutException) {
95+
} catch (e: IllegalStateException) {
96+
handleSshException(project, e)
97+
null
98+
} catch (e : TimeoutException){
9399
AdmWarningNotification("Don't forget to touch the yubikey if it blinks when using the AdmStorm plugin's features")
94100
.withTitle("Yubikey waiting timeout")
95101
.show()
96102
LOG.warn("Yubikey waiting timeout", e)
97103
null
98-
} catch (e: IllegalStateException) {
99-
handleSshException(project, e)
100-
null
101-
} catch (e: Exception) {
104+
}
105+
catch (e: Exception) {
102106
handleSshException(project, e)
103107
null
104108
} ?: return null
@@ -144,7 +148,6 @@ object MySshUtils {
144148
}
145149
)
146150
.show()
147-
148151
LOG.warn("Unexpected exception for execSync(builder)", e)
149152
}
150153

0 commit comments

Comments
 (0)