Skip to content

Commit 0c82a15

Browse files
committed
fix(container): handle Docker server errors and refactor build data creation #306
- Add error notifications for Docker server and context creation failures. - Refactor `createBuildData` method to handle constructor exceptions properly. - Simplify server filtering logic and improve code readability.
1 parent 0124003 commit 0c82a15

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

exts/ext-container/src/241/main/kotlin/cc/unitmesh/container/RunDevContainerService.kt

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.container
22

3+
import cc.unitmesh.devti.AutoDevNotifications
34
import cc.unitmesh.devti.provider.RunService
45
import com.intellij.clouds.docker.gateway.DockerDevcontainerDeployContext
56
import com.intellij.clouds.docker.gateway.ui.DockerDeployView
@@ -34,17 +35,25 @@ class RunDevContainerService : RunService {
3435
psiElement: PsiElement?,
3536
isFromToolAction: Boolean
3637
): String? {
37-
val server = dockerServers().firstOrNull() ?: return null
38+
val server = dockerServers().firstOrNull()
39+
if (server == null) {
40+
AutoDevNotifications.warn(project, "Cannot create DockerCloud server")
41+
return null
42+
}
3843
val projectDir = project.guessProjectDir()!!.toNioPath().toFile()
3944

40-
val devcontainerFile = File(projectDir, "devcontainer.json")
41-
devcontainerFile.writeText(virtualFile.contentsToByteArray().toString(Charsets.UTF_8))
45+
val containerFile = File(projectDir, "devcontainer.json")
46+
containerFile.writeText(virtualFile.contentsToByteArray().toString(Charsets.UTF_8))
47+
48+
val content = try {
49+
createContext(containerFile, projectDir, server)
50+
} catch (e: Exception) {
51+
AutoDevNotifications.error(project, "Cannot create DockerDevcontainerDeployContext")
52+
return null
53+
}
4254

43-
val content = createContext(devcontainerFile, projectDir, server)
4455
val wrapper = object : DialogWrapper(project) {
4556
override fun createCenterPanel(): JComponent? = BorderLayoutPanel()
46-
47-
4857
override fun beforeShowCallback() {
4958
val panel = contentPanel
5059
val lifetime = Lifetime.Companion.Eternal
@@ -67,8 +76,7 @@ class RunDevContainerService : RunService {
6776
val filteredServers =
6877
getDockerServers().filter {
6978
it.sshId == null
70-
}.nullize()
71-
?: listOf(createDefaultDockerServer("Local"))
79+
}.nullize() ?: listOf(createDefaultDockerServer("Local"))
7280
return filteredServers
7381
}
7482

@@ -88,27 +96,28 @@ class RunDevContainerService : RunService {
8896
return deployContext
8997
}
9098

91-
9299
private fun createBuildData(
93100
workingDir: File,
94101
modelFile: File,
95102
sources: File?
96103
): LocalBuildData {
97104
return try {
98-
val newConstructor: java.lang.reflect.Constructor<LocalBuildData> = LocalBuildData::class.java.getConstructor(
99-
File::class.java,
100-
File::class.java,
101-
Boolean::class.javaPrimitiveType
102-
)
103-
newConstructor.newInstance(modelFile, sources ?: workingDir, true)
104-
} catch (e: NoSuchMethodException) {
105-
val oldConstructor: java.lang.reflect.Constructor<LocalBuildData> = LocalBuildData::class.java.getConstructor(
106-
File::class.java,
107-
File::class.java,
108-
File::class.java,
109-
Boolean::class.javaPrimitiveType
110-
)
105+
val oldConstructor: java.lang.reflect.Constructor<LocalBuildData> =
106+
LocalBuildData::class.java.getConstructor(
107+
File::class.java,
108+
File::class.java,
109+
File::class.java,
110+
Boolean::class.javaPrimitiveType
111+
)
111112
oldConstructor.newInstance(workingDir, modelFile, sources, true)
113+
} catch (e: NoSuchMethodException) {
114+
val newConstructor: java.lang.reflect.Constructor<LocalBuildData> =
115+
LocalBuildData::class.java.getConstructor(
116+
File::class.java,
117+
File::class.java,
118+
Boolean::class.javaPrimitiveType
119+
)
120+
newConstructor.newInstance(modelFile, sources ?: workingDir, true)
112121
}
113122
}
114123
}

0 commit comments

Comments
 (0)