Skip to content

Commit 4d312e6

Browse files
committed
🤖 Add robust error handling for diff parsing in FileEditToolCall
- Add null checks for patches, hunks, and lines - Use optional chaining for safer property access - Provide fallback to raw diff display if parsing fails - Should fix Chromatic browser environment issues Generated with `cmux`
1 parent 2f7dfc2 commit 4d312e6

File tree

1 file changed

+68
-48
lines changed

1 file changed

+68
-48
lines changed

src/components/tools/FileEditToolCall.tsx

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -195,65 +195,85 @@ interface FileEditToolCallProps {
195195
function renderDiff(diff: string): React.ReactNode {
196196
try {
197197
const patches = parsePatch(diff);
198-
if (patches.length === 0) {
198+
if (!patches || patches.length === 0) {
199199
return (
200200
<DiffLine type="context">
201201
<LineContent type="context">No changes</LineContent>
202202
</DiffLine>
203203
);
204204
}
205205

206-
return patches.map((patch, patchIdx) => (
207-
<React.Fragment key={patchIdx}>
208-
{patch.hunks.map((hunk, hunkIdx) => {
209-
let oldLineNum = hunk.oldStart;
210-
let newLineNum = hunk.newStart;
206+
return patches.map((patch, patchIdx) => {
207+
if (!patch?.hunks) {
208+
return null;
209+
}
210+
211+
return (
212+
<React.Fragment key={patchIdx}>
213+
{patch.hunks.map((hunk, hunkIdx) => {
214+
if (!hunk?.lines) {
215+
return null;
216+
}
217+
218+
let oldLineNum = hunk.oldStart ?? 0;
219+
let newLineNum = hunk.newStart ?? 0;
211220

212-
return (
213-
<React.Fragment key={hunkIdx}>
214-
<DiffLine type="header">
215-
<DiffIndicator type="header">{/* Empty for alignment */}</DiffIndicator>
216-
<LineNumber type="header">{hunkIdx > 0 ? "⋮" : ""}</LineNumber>
217-
<LineContent type="header">
218-
@@ -{hunk.oldStart},{hunk.oldLines} +{hunk.newStart},{hunk.newLines} @@
219-
</LineContent>
220-
</DiffLine>
221-
{hunk.lines.map((line, lineIdx) => {
222-
const firstChar = line[0];
223-
const content = line.slice(1); // Remove the +/- prefix
224-
let type: DiffLineType = "context";
225-
let lineNumDisplay = "";
221+
return (
222+
<React.Fragment key={hunkIdx}>
223+
<DiffLine type="header">
224+
<DiffIndicator type="header">{/* Empty for alignment */}</DiffIndicator>
225+
<LineNumber type="header">{hunkIdx > 0 ? "⋮" : ""}</LineNumber>
226+
<LineContent type="header">
227+
@@ -{hunk.oldStart ?? 0},{hunk.oldLines ?? 0} +{hunk.newStart ?? 0},{hunk.newLines ?? 0} @@
228+
</LineContent>
229+
</DiffLine>
230+
{hunk.lines.map((line, lineIdx) => {
231+
if (!line || typeof line !== 'string') {
232+
return null;
233+
}
234+
235+
const firstChar = line[0];
236+
const content = line.slice(1); // Remove the +/- prefix
237+
let type: DiffLineType = "context";
238+
let lineNumDisplay = "";
226239

227-
if (firstChar === "+") {
228-
type = "add";
229-
lineNumDisplay = `${newLineNum}`;
230-
newLineNum++;
231-
} else if (firstChar === "-") {
232-
type = "remove";
233-
lineNumDisplay = `${oldLineNum}`;
234-
oldLineNum++;
235-
} else {
236-
// Context line
237-
lineNumDisplay = `${oldLineNum}`;
238-
oldLineNum++;
239-
newLineNum++;
240-
}
240+
if (firstChar === "+") {
241+
type = "add";
242+
lineNumDisplay = `${newLineNum}`;
243+
newLineNum++;
244+
} else if (firstChar === "-") {
245+
type = "remove";
246+
lineNumDisplay = `${oldLineNum}`;
247+
oldLineNum++;
248+
} else {
249+
// Context line
250+
lineNumDisplay = `${oldLineNum}`;
251+
oldLineNum++;
252+
newLineNum++;
253+
}
241254

242-
return (
243-
<DiffLine key={lineIdx} type={type}>
244-
<DiffIndicator type={type}>{firstChar}</DiffIndicator>
245-
<LineNumber type={type}>{lineNumDisplay}</LineNumber>
246-
<LineContent type={type}>{content}</LineContent>
247-
</DiffLine>
248-
);
249-
})}
250-
</React.Fragment>
251-
);
252-
})}
253-
</React.Fragment>
254-
));
255+
return (
256+
<DiffLine key={lineIdx} type={type}>
257+
<DiffIndicator type={type}>{firstChar}</DiffIndicator>
258+
<LineNumber type={type}>{lineNumDisplay}</LineNumber>
259+
<LineContent type={type}>{content}</LineContent>
260+
</DiffLine>
261+
);
262+
})}
263+
</React.Fragment>
264+
);
265+
})}
266+
</React.Fragment>
267+
);
268+
});
255269
} catch (error) {
256-
return <ErrorMessage>Failed to parse diff: {String(error)}</ErrorMessage>;
270+
console.error('Failed to parse diff:', error);
271+
// Fallback to raw diff display
272+
return (
273+
<pre style={{ margin: 0, whiteSpace: "pre-wrap", wordBreak: "break-word", fontSize: "11px" }}>
274+
{diff}
275+
</pre>
276+
);
257277
}
258278
}
259279

0 commit comments

Comments
 (0)