Skip to content

Commit 230a3af

Browse files
committed
fix(devti): handle file creation errors in planner result summary
#352 - Improve error handling when discarding changes or creating new files - Add logging for discarded change errors - Refactor file creation logic to handle different scenarios - Update error messages for file creation failures
1 parent 0cd223f commit 230a3af

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/planner/PlannerResultSummary.kt

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cc.unitmesh.devti.gui.planner
22

33
import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.AutoDevIcons
5+
import cc.unitmesh.devti.AutoDevNotifications
56
import cc.unitmesh.devti.util.relativePath
67
import com.intellij.diff.DiffContentFactoryEx
78
import com.intellij.diff.DiffContext
@@ -11,6 +12,7 @@ import com.intellij.icons.AllIcons
1112
import com.intellij.openapi.actionSystem.AnAction
1213
import com.intellij.openapi.actionSystem.AnActionEvent
1314
import com.intellij.openapi.application.runWriteAction
15+
import com.intellij.openapi.diagnostic.logger
1416
import com.intellij.openapi.fileEditor.FileDocumentManager
1517
import com.intellij.openapi.fileEditor.FileEditorManager
1618
import com.intellij.openapi.project.Project
@@ -73,8 +75,13 @@ class PlannerResultSummary(
7375

7476
private var globalActionListener: GlobalActionListener? = object : GlobalActionListener {
7577
override fun onDiscardAll() {
76-
rollbackWorker.doRollback(changes, false)
77-
updateChanges(mutableListOf())
78+
try {
79+
rollbackWorker.doRollback(changes, true)
80+
} catch (e: Exception) {
81+
logger<PlannerResultSummary>().warn("Failed to discard all changes: ${e.message}")
82+
} finally {
83+
updateChanges(mutableListOf())
84+
}
7885
}
7986

8087
override fun onAcceptAll() {
@@ -90,7 +97,12 @@ class PlannerResultSummary(
9097
}
9198

9299
override fun onDiscard(change: Change) {
93-
rollbackWorker.doRollback(listOf(change), false)
100+
try {
101+
rollbackWorker.doRollback(listOf(change), false)
102+
} catch (e: Exception) {
103+
logger<PlannerResultSummary>().warn("Failed to discard change: ${e.message}")
104+
}
105+
94106
val newChanges = changes.toMutableList()
95107
newChanges.remove(change)
96108
updateChanges(newChanges)
@@ -104,29 +116,41 @@ class PlannerResultSummary(
104116
when {
105117
file != null && content != null -> {
106118
val document = FileDocumentManager.getInstance().getDocument(file)
107-
document?.setText(content)
108-
}
109-
content != null -> {
110-
val afterFile = calculateNewPath(change)
111-
if (afterFile == null) {
112-
val message = AutoDevBundle.message("planner.error.no.after.file",)
113-
cc.unitmesh.devti.AutoDevNotifications.warn(project, message)
114-
return@runWriteAction
119+
if (document != null) {
120+
document.setText(content)
121+
} else {
122+
createNewFile(change, content)
115123
}
124+
}
116125

117-
getOrCreateDirectory(project.baseDir, afterFile)
118-
val fileName = afterFile.substringAfterLast('/')
119-
val newFile = project.baseDir?.createChildData(this, fileName)
120-
newFile?.let {
121-
it.setBinaryContent(content.toByteArray())
122-
FileEditorManager.getInstance(project).openFile(it, true)
123-
}
126+
content != null -> {
127+
createNewFile(change, content)
124128
}
125129
}
126130
}
127131
}
128132
}
129133

134+
private fun createNewFile(change: Change, content: String) {
135+
val afterFile = calculateNewPath(change)
136+
if (afterFile == null) {
137+
val message = AutoDevBundle.message("planner.error.no.after.file")
138+
AutoDevNotifications.warn(project, message)
139+
return
140+
}
141+
142+
getOrCreateDirectory(project.baseDir, afterFile)
143+
val fileName = afterFile.substringAfterLast('/')
144+
val newFile = project.baseDir?.createChildData(this, fileName)
145+
if (newFile != null) {
146+
newFile.setBinaryContent(content.toByteArray())
147+
FileEditorManager.getInstance(project).openFile(newFile, true)
148+
} else {
149+
val message = AutoDevBundle.message("planner.error.create.file", afterFile)
150+
AutoDevNotifications.warn(project, message)
151+
}
152+
}
153+
130154
private fun showDiffView(change: Change) {
131155
val diffRequest = runWriteAction { createDiffRequest(change) }
132156
val diffViewer = SimpleDiffViewer(object : DiffContext() {
@@ -160,9 +184,9 @@ class PlannerResultSummary(
160184

161185
private fun createDiffRequest(change: Change): SimpleDiffRequest {
162186
val diffFactory = DiffContentFactoryEx.getInstanceEx()
163-
val oldCode = change.beforeRevision?.content ?: "null"
187+
val oldCode = change.beforeRevision?.content ?: ""
164188
val newCode = try {
165-
change.afterRevision?.content ?: "null"
189+
change.afterRevision?.content ?: ""
166190
} catch (e: Exception) {
167191
"Error: ${e.message}"
168192
}

core/src/main/resources/messages/AutoDevBundle_en.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,4 @@ planner.action.discard.changes=Discard changes
202202
planner.action.accept.changes=Accept
203203
planner.error.no.file=No {0} file
204204
planner.error.no.after.file=No after file
205+
planner.error.create.file=Create file error {0}

core/src/main/resources/messages/AutoDevBundle_zh.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,5 @@ planner.action.discard.changes=丢弃变更
195195
planner.action.accept.changes=Accept
196196
planner.error.no.file=没有文件 {0}
197197
planner.error.no.after.file=No after file
198+
planner.error.create.file=Create file error {0}
198199

0 commit comments

Comments
 (0)