12
12
package com.redhat.devtools.gateway
13
13
14
14
import com.intellij.openapi.application.ApplicationManager
15
+ import com.intellij.openapi.application.EDT
15
16
import com.intellij.openapi.application.ModalityState
16
17
import com.intellij.openapi.diagnostic.thisLogger
17
18
import com.intellij.openapi.progress.ProcessCanceledException
@@ -29,6 +30,8 @@ import com.redhat.devtools.gateway.openshift.OpenShiftClientFactory
29
30
import com.redhat.devtools.gateway.openshift.kube.KubeConfigBuilder
30
31
import com.redhat.devtools.gateway.openshift.kube.isNotFound
31
32
import com.redhat.devtools.gateway.openshift.kube.isUnauthorized
33
+ import com.redhat.devtools.gateway.util.messageWithoutPrefix
34
+ import com.redhat.devtools.gateway.view.ui.Dialogs
32
35
import io.kubernetes.client.openapi.ApiException
33
36
import kotlinx.coroutines.CompletableDeferred
34
37
import kotlinx.coroutines.Dispatchers
@@ -67,9 +70,9 @@ class DevSpacesConnectionProvider : GatewayConnectionProvider {
67
70
indicator.setText(" Connecting to DevSpace..." )
68
71
indicator.setIndeterminate(true )
69
72
70
- val handle = doConnect(parameters, requestor, indicator) // suspend function assumed
73
+ val handle = doConnect(parameters, indicator)
71
74
72
- val thinClient = handle? .clientHandle
75
+ val thinClient = handle.clientHandle
73
76
? : throw RuntimeException (" Failed to obtain ThinClientHandle" )
74
77
75
78
indicator.setText(" Waiting for remote IDE to start..." )
@@ -86,31 +89,32 @@ class DevSpacesConnectionProvider : GatewayConnectionProvider {
86
89
onClientClosed(readyDeferred, indicator))
87
90
88
91
readyDeferred.await()
89
- } catch (err : ApiException ) {
92
+ } catch (e : ApiException ) {
90
93
indicator.setText(" Connection failed" )
91
- Timer (2000 ) {
92
- indicator.close(DialogWrapper .CANCEL_EXIT_CODE )
93
- }.start()
94
-
95
- if (handleUnauthorizedError(err) || handleNotFoundError(err)) {
96
- null
97
- } else {
98
- throw err
94
+ delayedClose(indicator)
95
+ if (! (handleUnauthorizedError(e) || handleNotFoundError(e))) {
96
+ Dialogs .error(e.messageWithoutPrefix() ? : " Could not connect to workspace." , " Connection Error" )
99
97
}
98
+ null
100
99
} catch (e: Exception ) {
101
100
indicator.setText(" Unexpected error: ${e.message} " )
102
- Timer (2000 ) {
103
- indicator.close(DialogWrapper .CANCEL_EXIT_CODE )
104
- }.start()
105
- throw e
101
+ delayedClose(indicator)
102
+ Dialogs .error(e.messageWithoutPrefix() ? : " Could not connect to workspace." , " Connection Error" )
103
+ null
106
104
} finally {
107
- ApplicationManager .getApplication().invokeLater {
105
+ withContext( Dispatchers . EDT ) {
108
106
if (indicator.isShowing) indicator.close(DialogWrapper .OK_EXIT_CODE )
109
107
}
110
108
}
111
109
}
112
110
}
113
111
112
+ private fun delayedClose (indicator : StartupProgressIndicator ) {
113
+ Timer (2000 ) {
114
+ indicator.close(DialogWrapper .CANCEL_EXIT_CODE )
115
+ }.start()
116
+ }
117
+
114
118
private fun onClientPresenceChanged (
115
119
readyDeferred : CompletableDeferred <GatewayConnectionHandle ?>,
116
120
indicator : StartupProgressIndicator ,
@@ -162,9 +166,8 @@ class DevSpacesConnectionProvider : GatewayConnectionProvider {
162
166
@Throws(IllegalArgumentException ::class )
163
167
private fun doConnect (
164
168
parameters : Map <String , String >,
165
- requestor : ConnectionRequestor ,
166
169
indicator : ProgressIndicator ? = null
167
- ): GatewayConnectionHandle ? {
170
+ ): GatewayConnectionHandle {
168
171
thisLogger().debug(" Launched Dev Spaces connection provider" , parameters)
169
172
170
173
indicator?.text2 = " Preparing connection environment…"
@@ -224,12 +227,10 @@ class DevSpacesConnectionProvider : GatewayConnectionProvider {
224
227
" \n\n You are using token-based authentication.\n Update your token in the kubeconfig file."
225
228
else " "
226
229
227
- withContext(Dispatchers .Main ) {
228
- Messages .showErrorDialog(
229
- " Your session has expired.\n Please log in again to continue.$tokenNote " ,
230
- " Authentication Required"
231
- )
232
- }
230
+ Dialogs .error(
231
+ " Your session has expired.\n Please log in again to continue.$tokenNote " ,
232
+ " Authentication Required"
233
+ )
233
234
return true
234
235
}
235
236
@@ -243,9 +244,7 @@ class DevSpacesConnectionProvider : GatewayConnectionProvider {
243
244
Please verify your Kubernetes context, namespace, and that the DevWorkspace Operator is installed and running.
244
245
""" .trimIndent()
245
246
246
- withContext(Dispatchers .Main ) {
247
- Messages .showErrorDialog(message, " Resource Not Found" )
248
- }
247
+ Dialogs .error(message, " Resource Not Found" )
249
248
return true
250
249
}
251
250
0 commit comments