fix: handle grep command errors and return user-friendly message for no matches

This commit is contained in:
Nawaz Dhandala
2025-12-09 14:16:28 +00:00
parent cc9e8f174a
commit de6a58009a

View File

@@ -155,11 +155,23 @@ export class SearchWorkspaceTool extends StructuredTool<SearchArgs> {
".",
];
return Execute.executeCommandFile({
command: "grep",
args: finalArgs,
cwd,
});
try {
return await Execute.executeCommandFile({
command: "grep",
args: finalArgs,
cwd,
});
} catch (error) {
// grep exits with code 1 when no matches are found - treat this as empty result
const errorMessage: string = (error as Error).message || "";
if (
errorMessage.includes("exit code 1") ||
errorMessage.includes("Command failed")
) {
return "No matches found";
}
throw error;
}
}
/** Formats CLI output with context about scope and engine. */