mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-22 18:24:21 +01:00
fix: handle parsePatch errors in TUI to prevent crashes
Wrap parsePatch calls in try-catch blocks to gracefully handle malformed diffs that can occur when undoing after tool_use/tool_result errors or cancelled prompts. Prevents TUI from crashing with 'Added line count did not match for hunk' error. Fixes #3700
This commit is contained in:
@@ -480,6 +480,7 @@ export function Session() {
|
|||||||
const diffText = s.revert?.diff || ""
|
const diffText = s.revert?.diff || ""
|
||||||
if (!diffText) return []
|
if (!diffText) return []
|
||||||
|
|
||||||
|
try {
|
||||||
const patches = parsePatch(diffText)
|
const patches = parsePatch(diffText)
|
||||||
return patches.map((patch) => {
|
return patches.map((patch) => {
|
||||||
const filename = patch.newFileName || patch.oldFileName || "unknown"
|
const filename = patch.newFileName || patch.oldFileName || "unknown"
|
||||||
@@ -496,6 +497,9 @@ export function Session() {
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} catch (error) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
})()
|
})()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -1245,6 +1249,8 @@ ToolRegistry.register<typeof EditTool>({
|
|||||||
const diff = createMemo(() => {
|
const diff = createMemo(() => {
|
||||||
const diff = props.metadata.diff ?? props.permission["diff"]
|
const diff = props.metadata.diff ?? props.permission["diff"]
|
||||||
if (!diff) return null
|
if (!diff) return null
|
||||||
|
|
||||||
|
try {
|
||||||
const patches = parsePatch(diff)
|
const patches = parsePatch(diff)
|
||||||
if (patches.length === 0) return null
|
if (patches.length === 0) return null
|
||||||
|
|
||||||
@@ -1298,6 +1304,9 @@ ToolRegistry.register<typeof EditTool>({
|
|||||||
oldContent: oldLines.join("\n"),
|
oldContent: oldLines.join("\n"),
|
||||||
newContent: newLines.join("\n"),
|
newContent: newLines.join("\n"),
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const code = createMemo(() => {
|
const code = createMemo(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user