Skip to content

Commit 6bad2c2

Browse files
committed
fix(compiler): handle exceptions in patch application
Wrap patch application logic in a try-catch block to handle exceptions and return a meaningful error message using DEVINS_ERROR. This prevents unhandled exceptions from crashing the process and improves error reporting.
1 parent 99bac5a commit 6bad2c2

File tree

1 file changed

+25
-20
lines changed
  • exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec

1 file changed

+25
-20
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/PatchInsCommand.kt

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cc.unitmesh.devti.language.compiler.exec
22

33
import cc.unitmesh.devti.devin.InsCommand
44
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
5+
import cc.unitmesh.devti.language.compiler.error.DEVINS_ERROR
56
import com.intellij.openapi.application.ApplicationManager
67
import com.intellij.openapi.application.runInEdt
78
import com.intellij.openapi.diff.impl.patch.FilePatch
@@ -24,34 +25,38 @@ class PatchInsCommand(val myProject: Project, val prop: String, val codeContent:
2425
FileDocumentManager.getInstance().saveAllDocuments()
2526
}
2627

27-
val myReader = PatchReader(codeContent)
28-
myReader.parseAllPatches()
28+
try {
29+
val myReader = PatchReader(codeContent)
30+
myReader.parseAllPatches()
2931

30-
val filePatches: MutableList<FilePatch> = myReader.allPatches
32+
val filePatches: MutableList<FilePatch> = myReader.allPatches
3133

32-
ApplicationManager.getApplication().invokeAndWait {
33-
val matchedPatches = MatchPatchPaths(myProject).execute(filePatches, true)
34+
ApplicationManager.getApplication().invokeAndWait {
35+
val matchedPatches = MatchPatchPaths(myProject).execute(filePatches, true)
3436

35-
val patchGroups = MultiMap<VirtualFile, AbstractFilePatchInProgress<*>>()
36-
for (patchInProgress in matchedPatches) {
37-
patchGroups.putValue(patchInProgress.base, patchInProgress)
38-
}
37+
val patchGroups = MultiMap<VirtualFile, AbstractFilePatchInProgress<*>>()
38+
for (patchInProgress in matchedPatches) {
39+
patchGroups.putValue(patchInProgress.base, patchInProgress)
40+
}
3941

40-
if (patchGroups.isEmpty) return@invokeAndWait
41-
/// open file in editor
42-
filePatches.firstOrNull()?.apply {
43-
val file = myProject.guessProjectDir()!!.findFileByRelativePath(this.beforeFileName.toString())
44-
file?.let {
45-
ApplicationManager.getApplication().invokeAndWait {
46-
FileEditorManager.getInstance(myProject).openFile(it, true)
42+
if (patchGroups.isEmpty) return@invokeAndWait
43+
/// open file in editor
44+
filePatches.firstOrNull()?.apply {
45+
val file = myProject.guessProjectDir()!!.findFileByRelativePath(this.beforeFileName.toString())
46+
file?.let {
47+
ApplicationManager.getApplication().invokeAndWait {
48+
FileEditorManager.getInstance(myProject).openFile(it, true)
49+
}
4750
}
4851
}
52+
53+
val additionalInfo = myReader.getAdditionalInfo(ApplyPatchDefaultExecutor.pathsFromGroups(patchGroups))
54+
ApplyPatchDefaultExecutor(myProject).apply(filePatches, patchGroups, null, prop, additionalInfo)
4955
}
5056

51-
val additionalInfo = myReader.getAdditionalInfo(ApplyPatchDefaultExecutor.pathsFromGroups(patchGroups))
52-
ApplyPatchDefaultExecutor(myProject).apply(filePatches, patchGroups, null, prop, additionalInfo)
57+
return "Applied ${filePatches.size} patches."
58+
} catch (e: Exception) {
59+
return "$DEVINS_ERROR message: " + e.message
5360
}
54-
55-
return "Applied ${filePatches.size} patches."
5661
}
5762
}

0 commit comments

Comments
 (0)